Why does the system call onSaveInstanceState() when the activity enters the stop state?
Since the activity's instance s kept resident in memory when it is stopped, and its current state is lost only when the activity is destroyed.
So the system should rather call the onSaveInstanceState() before entering or inside onDestroy() rather than onStop().
Isn't it?
onPause() is always called. This is guaranteed. If you need to save any state in your activity you need to save it in onPause() . onStop() may be called after onPause() , or it may not.
onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.
From the documentation I understand that onSaveInstanceState() should be called to store only temporary information, and onPause() should be used to store any persistent data.
I'm aware that onSaveInstanceState() is not always called when an activity is about to be destroyed. For example, if it is destroyed because the user has pressed the "back" button, the activity state is not preserved.
Because onDestroy()
is not guaranteed to be called (generally, only if you finish()
your activity) and because after onStop()
, your activity is not guaranteed to remain. It might be destroyed for any number of reasons once it is not the foreground activity.
I always assume that after onPause()
, the next callback to be made might be onCreate()
, that way, I never get surprised ;)
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