Android 保留状态不一致
Android preserving state inconsistencies
作为一名新的 android 开发人员,我正在尝试采用 android 的思维方式。我在保留 activity 的状态时遇到了问题,因为 onSavedInstanceState 在 Android.
的不同版本之间似乎无法一致地工作
在版本 3 之前,onSavedInstanceState 会保留数据,即使应用程序通过手动将其从最近使用的应用程序列表中删除而被杀死。在较新的版本中情况并非如此。另一种方法是使用共享首选项,但 activity 的生命周期似乎相当复杂。
我可以使用 onPause/onResume 方法,但每次都会调用这些方法,甚至在 activity 的创建过程中也是如此。一个简单的解决方案是使用一个布尔标志来检查onCreate事件之前是否有运行,但感觉不对。
您建议什么是在所有情况下保持状态的最佳策略(例如:应用程序被手动杀死,应用程序被 OS 杀死,activity 被另一个覆盖,等等)?
提前致谢。
Note: Because onSaveInstanceState() is not guaranteed to be called,
you should use it only to record the transient state of the activity
(the state of the UI)—you should never use it to store persistent
data. Instead, you should use onPause() to store persistent data (such
as data that should be saved to a database) when the user leaves the
activity.
作为一名新的 android 开发人员,我正在尝试采用 android 的思维方式。我在保留 activity 的状态时遇到了问题,因为 onSavedInstanceState 在 Android.
的不同版本之间似乎无法一致地工作在版本 3 之前,onSavedInstanceState 会保留数据,即使应用程序通过手动将其从最近使用的应用程序列表中删除而被杀死。在较新的版本中情况并非如此。另一种方法是使用共享首选项,但 activity 的生命周期似乎相当复杂。
我可以使用 onPause/onResume 方法,但每次都会调用这些方法,甚至在 activity 的创建过程中也是如此。一个简单的解决方案是使用一个布尔标志来检查onCreate事件之前是否有运行,但感觉不对。
您建议什么是在所有情况下保持状态的最佳策略(例如:应用程序被手动杀死,应用程序被 OS 杀死,activity 被另一个覆盖,等等)?
提前致谢。
Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.