Android Activity生命周期
1、
Android 面试:如何理解 Activity 的生命周期
2、
3、
onPause onPause方法是我绝对想跟大家强调的一个方法,首先onPause方法绝对不可以进行太耗时的操作,或者一些重量级的释放操作,因为这会影响下一个Activity进入前台与用户交互。也就是说,只有onPause方法调用完毕,下一个Activity的onStart才会调用。
在一些永久数据保存上,找到了这样的一段描述:
onPause(), onStop(), onDestroy() are "killable after" lifecycle methods. This indicates whether or not the system can kill the
process hosting the activity at any time after the method returns,
without executing another line of the activity's code. Because
onPause() is the first of the three, once the activity is created,
onPause() is the last method that's guaranteed to be called before
the process can be killed—if the system must recover memory in an
emergency, then onStop() and onDestroy() might not be called.
Therefore, you should use onPause() to write crucial persistent data
(such as user edits) to storage. However, you should be selective
about what information must be retained during onPause(), because
any blocking procedures in this method block the transition to the
next activity and slow the user experience.
翻译过来就是:无论出现怎样的情况,比如程序突然死亡了,能保证的就是onPause方法是一定会调用的,而onStop和onDestory方法并不一定,所以这个特性使得onPause是持久化相关数据的最后的可靠时机。
所以比如你要写入数据库的数据,就可以放到onPause保存,关于一些其他细节,推荐大家看一看我以前总结的一篇文章:
浙公网安备 33010602011771号