onSaveInstanceState() and onPause() call sequence
Asked Answered
G

3

5

The documentation on onSaveInstanceState() states:

If the method is called, it is always called before onStop() and possibly before onPause().

But, I notice, consistently, from log messages that onPause() is ALWAYS CALLED BEFORE onSaveInstanceState(). I had put log messages in these two methods. Please help me understand in what circumstances does onSaveInstanceState() is called before onPause().

Environment: Android v4.0 (API 14) + Eclipse v3.7.1 - Indigo.

Geniculate answered 9/1, 2012 at 6:21 Comment(1)
Please note that clarification was sought for sequence only. Regarding where to save, there are many good posts.Geniculate
T
8

You can read about that here.

In a nutshell you can't never know about time when onSaveInstanceState will be run.

Tildi answered 9/1, 2012 at 7:7 Comment(0)
U
3

Please help me understand in what circumstances does onSaveInstanceState() is called before onPause()

There is a difference in the Activity lifecycle between the pre-HONEYCOMB and the other platforms (since HONEYCOMB onwards):

API level >= 11: when onPause() is called, the process is in a safe state, it can't be killed.

API level < 11 : when onPause() is called, the process that hosts the Activity becomes killable. It means that the system can kill the process, that contains the activity, without executing any other line of code. So if this happens the onSaveInstanceState() may never be called. In order to avoid this, the system should call onSaveInstanceState() before onPause(), otherwise you will not able to save the user state.

Uxmal answered 16/2, 2015 at 20:20 Comment(1)
Speculating about which order the system should call the two is irrelevant: The important point is that the docs don't guarantee which order the two calls occur in. Nevertheless, this answer (though it does not answer the question) is somewhat useful in pointing out a difference between pre- and post- 11 builds.Gavette
U
0

onSaveInstanceState() is nice, but only guaranted callback is onPause(), called when your activity loses focus. So, save your state there

Underpants answered 9/1, 2012 at 7:41 Comment(4)
Note that onSaveInstanceState() IS NOT ALWAYS called when an activity loses focus. For more information, please refer to link in above post and "Saving activity state" sub-topic @ developer.android.com/guide/topics/fundamentals/activities.htmlGeniculate
For saving Bundle state, onSaveInstanceState is what you should use. While it is true that onPause will always be called, for typical bundle state, it is wasteful to save it on every pause. If it is possible to save bundle state before killing an Activity, onSaveInstanceState will be called and that call will happen before onStop. Of course, if you have some action that truly needs to be performed every time an Activity is paused, onPause is the right place for that.Sherronsherry
Never save state in onPause.Cheat
IMHO, there may be two different meanings of "state" being discussed here. onSaveInstanceState is handy as a high-performance way to save some UI state during a temporary pause of app. No guarantee that it will be called. (See android dev "Activities" page.) Its just a convenience to preserve the state of the UI that the user was seeing. Don't rely on it for anything important.Gavette

© 2022 - 2024 — McMap. All rights reserved.