问题
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.
回答1:
You can read about that here.
In a nutshell you can't never know about time when onSaveInstanceState will be run.
回答2:
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.
回答3:
onSaveInstanceState() is nice, but only guaranted callback is onPause(), called when your activity loses focus. So, save your state there
来源:https://stackoverflow.com/questions/8784610/onsaveinstancestate-and-onpause-call-sequence