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.

Sam
  • 86,580
  • 20
  • 181
  • 179
lupchiazoem
  • 8,026
  • 6
  • 36
  • 42
  • Please note that clarification was sought for sequence only. Regarding where to save, there are many good posts. – lupchiazoem Jan 09 '12 at 07:53

3 Answers3

8

You can read about that here.

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

RagnarR
  • 192
  • 7
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.

GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • 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. – ToolmakerSteve Sep 26 '15 at 17:12
0

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

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • 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 @ http://developer.android.com/guide/topics/fundamentals/activities.html – lupchiazoem Jan 09 '12 at 08:00
  • 2
    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. – dgh Jul 02 '14 at 08:42
  • Never save state in onPause. – Eugene Chumak Nov 28 '14 at 17:44
  • 1
    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. – ToolmakerSteve Sep 26 '15 at 17:01