I pass a boolean to my Activity when I create it via an Intent BundleExtra. Now looking at the activity lifecycle, if my activity gets stopped (onStop
), then another app needs memory so the app process is killed, then the user navigates to the activity (onCreate
). Will the last onCreate contain my original boolean I passed? I would assume if I wanted that boolean to be saved I would need to save it in OnSaveInstanceState
, correct?
3 Answers
Actually, when your activity is recreated, the original intent will still be used. getIntent()
will return the same intent as it did when it was first created. However, if you have other data that you want to preserve when the activity is recreated, you will need to save it using saveInstanceState()
. You can verify this by simply rotating the device with an activity running, as it will be destroyed and recreated with the same intent. For more information, see here.

- 4,668
- 34
- 35
I would use onPause() for this reason (from the docs)
Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.
Then read it back again in onCreate()
e.g. from database or some other resource you stored it in.
So depending on how important that boolean value is you will use saving mechanism you want.. for persistent state: http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
And for UI state such as simple texts, selections use onSaveInstanceState
like described here: Saving Android Activity state using Save Instance State
As an summary: when process killed boolean = gone if not saved :)

- 1
- 1

- 9,688
- 3
- 33
- 54
-
perfect. The As a summary is exactly what I was looking for. I actually want that boolean to be gone so I can track where the onCreate is being called from – jsb Aug 24 '12 at 15:37
Mauno V. is right, in your case you must go with onPause()
The fact is onSaveInstanceState(Bundle bundle) is designed to save state in your current instance. So when your app is killed, the saved bundle too
You can use SharedPreferences to save your boolean :
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
sharedPreferences.edit().putBoolean("hiBoolean", booleanValue).commit();
And retrieve it on your onCreate :
Boolean hiBoolean = sharedPreferences.getBoolean("hiBoolean", true);

- 1,659
- 1
- 16
- 31
-
This is not correct. When the process hosting your app will be killed, the "save instance state bundle" will be preserved by ActivityManager and will be passed back when the user navigates to your application. The Android will create a process and instantiate the application and then will create the activity that was on top of the stack and then pass in the last saved instance state bundle. Earlier this bundle could not be preserved across device reboots, but Android is getting awesome day by day and starting sdk level 21, the bundle is preserved across reboots too. – Jai Pandit Jul 14 '17 at 21:00