android– 什么时候调用onSaveInstanceState()和onRestoreInstanceState()?

android– 什么时候调用onSaveInstanceState()和onRestoreInstanceState()?,第1张

概述下图(来自theofficialdoc)描述了Android活动的众所周知的生命周期:另一方面,当系统销毁活动时(例如因为需要回收内存),活动的状态有时会通过onSaveInstanceState()和onRestoreInstanceState()方法自动保存和恢复,如图所示如下图(也来自theofficialdoc):我知道当一个活动即将被

下图(来自the official doc)描述了AndroID活动的众所周知的生命周期:

另一方面,当系统销毁活动时(例如因为需要回收内存),活动的状态有时会通过onSaveInstanceState()和onRestoreInstanceState()方法自动保存和恢复,如图所示如下图(也来自the official doc):

我知道当一个活动即将被销毁时,onSaveInstanceState()是not always called.例如,如果由于用户按下“后退”按钮而导致销毁,则不会保留活动状态.但是在保存和恢复状态的情况下,调用onSaveInstanceState()/ onRestoreInstanceState()时,它们究竟被调用了吗?

例如,根据上面的图,onRestoreInstanceState()可能在onStart()之前,或在onStart()之后但在onResume()之前或onResume()之后被调用.类似地,onSaveInstanceState()存在几种可能性.那他们什么时候打电话呢?

理想情况下,我希望看到一个组合图,显示活动生命周期状态和保存/恢复方法(如果存在).

解决方法:

按documentation:

voID onRestoreInstanceState (Bundle savedInstanceState)

This method is called between onStart() and onPostCreate(Bundle).

voID onSaveInstanceState (Bundle outState)

If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlIEr platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().

总结

以上是内存溢出为你收集整理的android – 什么时候调用onSaveInstanceState()和onRestoreInstanceState()?全部内容,希望文章能够帮你解决android – 什么时候调用onSaveInstanceState()和onRestoreInstanceState()?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102737.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存