不保证调用onSaveInstanceState()

时间:2013-12-28 13:53:51

标签: android

我已从Android文档

中读到以下声明
  

因为无法保证调用onSaveInstanceState(),所以   应该只用它来记录活动的瞬态(   UI的状态) - 你永远不应该用它来存储持久数据

由于无法保证调用onSaveInstanceState()。我们如何依靠它来保存数据?是否有任何特殊情况不会被召唤?

2 个答案:

答案 0 :(得分:2)

由于nPn已经给出了链接。阅读以下内容

(参考:http://developer.android.com/reference/android/app/Activity.html

One example of when onPause() and onStop() is called and not this method is when a user 
navigates back from activity B to activity A: there is no need to call 
onSaveInstanceState(Bundle) on B because that particular instance will never be restored,
so the system avoids calling it. An example when onPause() is called and not 
onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the 
system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed 
during the lifetime of B since the state of the user interface of A will stay intact.

答案 1 :(得分:1)

我会使用onPause来保存对象数据。 请参阅此活动生命周期图 http://developer.android.com/training/basics/activity-lifecycle/starting.html

注意:您仍应使用onSaveInstanceState()和onRestoreInstanceState()。这些是您保存和恢复应用程序状态的机会,例如旋转屏幕并杀死应用程序然后重新启动。