The documentation on onSaveInstanceState()
states:
If the method is called, it is always called before onStop() and possibly before onPa
onSaveInstanceState() is nice, but only guaranted callback is onPause(), called when your activity loses focus. So, save your state there
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.
You can read about that here.
In a nutshell you can't never know about time when onSaveInstanceState will be run.