I'm trying to save and restore the state of an Activity
using the methods onSaveInstanceState()
and onRestoreInstanceState()
.
The problem is that it never enters the onRestoreInstanceState()
method. Can anyone explain to me why this is?
I'm trying to save and restore the state of an Activity
using the methods onSaveInstanceState()
and onRestoreInstanceState()
.
The problem is that it never enters the onRestoreInstanceState()
method. Can anyone explain to me why this is?
I think this thread was quite old. I just mention another case, that
onSaveInstanceState()
will also be called, is when you callActivity.moveTaskToBack(boolean nonRootActivity)
.It is not necessary that onRestoreInstanceState will always be called after onSaveInstanceState.
Note that : onRestoreInstanceState will always be called, when activity is rotated (when orientation is not handled) or open your activity and then open other apps so that your activity instance is cleared from memory by OS.
The state you save at
onSaveInstanceState()
is later available atonCreate()
method invocation. So useonCreate
(and itsBundle
parameter) to restore state of your activity.In my case,
onRestoreInstanceState
was called when the activity was reconstructed after changing the device orientation.onCreate(Bundle)
was called first, but the bundle didn't have the key/values I set withonSaveInstanceState(Bundle)
.Right after,
onRestoreInstanceState(Bundle)
was called with a bundle that had the correct key/values.onRestoreInstanceState()
is called only when recreating activity after it was killed by the OS. Such situation happen when:onRestoreInstanceState()
will be called.In contrast: if you are in your activity and you hit
Back
button on the device, your activity is finish()ed (i.e. think of it as exiting desktop application) and next time you start your app it is started "fresh", i.e. without saved state because you intentionally exited it when you hitBack
.Other source of confusion is that when an app loses focus to another app
onSaveInstanceState()
is called but when you navigate back to your apponRestoreInstanceState()
may not be called. This is the case described in the original question, i.e. if your activity was NOT killed during the period when other activity was in frontonRestoreInstanceState()
will NOT be called because your activity is pretty much "alive".All in all, as stated in the documentation for
onRestoreInstanceState()
:As I read it: There is no reason to override
onRestoreInstanceState()
unless you are subclassingActivity
and it is expected that someone will subclass your subclass.As a workaround, you could store a bundle with the data you want to maintain in the Intent you use to start activity A.
Activity A would have to pass this back to Activity B. You would retrieve the intent in Activity B's onCreate method.
Another idea is to create a repository class to store activity state and have each of your activities reference that class (possible using a singleton structure.) Though, doing so is probably more trouble than it's worth.