For instance, if I call activity B from A, how does Android system save the state of activity A? Is it saved using onsaveinstancestate()
method? If so, when activity A gets restored, what are the sequence of methods called to restore the state of activity A. I understand that activity A goes through Onpause()
and) OnStop()
method when activity B is called and onRestart()
method is called once activity A needs to be restored. But how exactly is the state of activity A restored?

- 6,721
- 6
- 38
- 63
2 Answers
This image shows all Activity statuses
This link explain well how to store infos and retriveve them when the activity is restored.
protected void onCreate (Bundle savedInstanceState)
Called when the activity is starting. This is where most initialization should go.
Parameters
savedInstanceState If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle)
. Note: Otherwise it is null
.
protected void onRestoreInstanceState (Bundle savedInstanceState)
This method is called after onStart()
when the activity is being re-initialized from a previously saved state, given here in savedInstanceState
. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle)
.
-
I can clearly understand the flowchart given above as I have briefly mentioned about the life cycle the activities go thru, in my question. My question is when onSaveinstancestate is called and onrestoreinstancestate is called? Is it used by the android system to restore the state of an activity that is present in the back stack? For eg: Activity A calls Activity B and then when you press back button while in Activity B, activity A is restored. How Android restores the state of Activity A? – user2048186 Feb 07 '13 at 07:12
-
If activity A is shutdown or process killed, everything can be restored from bundle in onCreate (Bundle savedInstanceState). Checkout my improved answer. – StarsSky Feb 07 '13 at 08:26
It doesn't. You do. Use onPause() to write your application state. onResume() to restore it. Use the Preferences to store and load variable values.

- 4,547
- 6
- 35
- 42
-
Yes, I do understand that Preferences can be used to save persistent data; Just curious to know the use of onsaveinstancestate and how android uses it. Is it only used for restoring activities killed by itself? Or it is also used to restore activities pushed down in the back stack? – user2048186 Feb 07 '13 at 07:15