Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If an activity is stopped then recreated what gets passed into getIntent()

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?

like image 438
jsb Avatar asked Aug 24 '12 14:08

jsb


2 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.

like image 106
Tad Avatar answered Oct 11 '22 18:10

Tad


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 :)

like image 22
Mauno Vähä Avatar answered Oct 11 '22 18:10

Mauno Vähä