onSaveInstanceState() and onPause() call sequence

戏子无情 提交于 2019-12-19 19:49:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!