Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否有自然的地方来保存 Android 应用程序关闭时的状态?目前我正在onDestroy()为我的主要活动做这件事,然后onCreate()为同样的活动重新加载。那是合适的地方吗?
onDestroy()
onCreate()
应用程序不会在任何传统意义上“关闭”。您的 UI 可能会移至后台,并且您的进程最终将被终止。
在许多情况下,保存应用状态的正确时间是状态发生变化的时候。在最坏的情况下,当 Activity 移动到后台时(即 in或)保存Activity的状态。onPause()onStop()
onPause()
onStop()
onDestroy()不保证在任何组件上调用,包括活动。因此,它不是保存状态的合适位置。
另请注意,保存状态需要后台线程,对于术语“保存”的任何典型定义,如果您不想丢帧并冻结您的 UI。