Reputation: 5417
I have read the below statement from android document
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
Since onSaveInstanceState()
is not guaranteed to be called. How can we rely on that to save data? is there any particular situation where it won't be called?
Upvotes: 1
Views: 689
Reputation: 2891
As nPn has already given the link. Read below lines
(Reference :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.
Upvotes: 2
Reputation: 16768
I would use onPause to persist object data. See this activity life cycle diagram http://developer.android.com/training/basics/activity-lifecycle/starting.html
Note: you should still use onSaveInstanceState() and onRestoreInstanceState(). These are your opportunity to save and restore the state of your application when for example the screen is rotated and the app is killed and then restarted.
Upvotes: 1