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?
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With