Are onCreate and onRestoreInstanceState mutually exclusive?
I have a couple of question regarding onRestoreInstanceState
and onSaveInstanceState
.
1) where do these methods fit the activity lifecycle? I have read a lot of documentation but there is no clear idea, except a broad statement that when the state of the activity is to be saved
2) Are onCreate
and onRestoreInstanceState
mutually exclusive?
3) is onRestoreInstanceState
called when the activity is destroyed? what does this mean? an activity always destroyed except for scenarios when another activity is floating on top of current.
4) onRestoreInstanceState
appears to be called only from instrumentation in jelly bean. Is this no longer relevant to activity lifecycle?
Solution 1:
where do these methods fit the activity lifecycle?
OnSaveInstanceState is called right before your activity is about to be killed or restarted (e.g b/c of memory pressure or configuration change). Note that this is different from onPause which gets called when your activity loses focus (e.g you transition to another activity).
Usually onSaveInstanceState will be called after onPause but before onStop but not always. E.g if you press back, then the activity is destroyed (like calling finish()) and there is no need to save state so onSaveInstanceState is not called. So why not just save state in onPause? Just because the activity loses focus doesn't mean it has been killed. It is still in memory. Basically you don't want to save state every time you are paused but rather when you are paused and about to become invisible (i.e go from foreground to background).
So what should you do in onPause? Ideally you should release resources that drain your battery e.g network connections, turn off geo or accelerometer, pause a video (all of this depends on your app). And restore these resources in onResume which, as you might have guessed, gets called when your activity gains focus.
Are onCreate and onRestoreInstanceState mutually exclusive?
onRestoreInstanceState is redundant because you can easily restore state in onCreate.
Having said that here is what the official doc says for onRestoreInstanceState:
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.
So for best practice, lay out your view hierarchy in onCreate and restore the previous state in onRestoreInstanceState. If you do that, anyone who subclasses your Activity can chose to override your onRestoreInstanceState to augment or replace your restore state logic. This is a long way of saying onRestoreInstanceState serves as a template method.
is onRestoreInstanceState called when the activity is destroyed? what does this mean?
This was partially answered in 1. Yes, onRestore gets called when the system is about to destroy your activity. The system will destroy your activity when it is under memory pressure or the user explicitly closes the application (e.g swipe-delete from recents in nav bar) or there is a configuration change (e.g landspace to portrait).
Why is android designed like this (unlike desktop apps)? Because on mobile systems, resource management is an acute problem b/c of battery life. So you want to provide hooks into app lifecyle so that the app can cleanly save and restore their state between shutdowns or losing focus while at the same time making it totally transparant to the user.
onRestoreInstanceState appears to be called only from instrumentation in jelly bean. Is this no longer relevant to activity lifecycle?
I don't understand this question. Can you rephrase it?
Solution 2:
1) where do these methods fit the activity lifecycle?
from the developer docs.
onSaveInstanceState (Bundle outState)
This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).
The default implementation of onSaveInstanceState() takes care of saving the data related with each n every view that is having an id.
If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().
onRestoreInstanceState (Bundle savedInstanceState)
This method is called after onStart() when the activity is being re-initialized from a previously saved state
3) is onRestoreInstanceState called when the activity is destroyed? what does this mean? an activity always destroyed except for scenarios when another activity is floating on top of current.
This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState (which is a bundle object containing data saved in onSaveInstanceState(Bundle)).
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).
4) onRestoreInstanceState appears to be called only from instrumentation in jelly bean. Is this no longer relevant to activity lifecycle?
No. onRestoreInstanceState has been there since API level 1. And its still part of the new Jelly Bean API.