Android-如何在恢复时检测到我的应用程序被 VM 杀死? [英] Android-How to detect on resume that my app was killed by VM?
问题描述
在我的应用程序中,当用户按下 HOME 键并在一段时间后返回应用程序时,我的应用程序会在各种位置和活动上提供 NullPointerExceptions.我知道我的应用程序被操作系统杀死以释放一些资源.现在我希望当用户再次返回应用程序并且应用程序之前被杀死时,那么我如何检测我的应用程序被杀死以便我可以重新加载不同的资源?
In my app, when user presses HOME key and returns back to app after some time, my app gives NullPointerExceptions on various places and activities. I know that my app is being killed by OS to free some resources. Now I want that when user comes back again to app and app was previously killed, then how can I detect that my app was killed so that I can reload different resources?
推荐答案
当onCreate (Bundle savedInstanceState)
被调用时,检查savedInstanceState
是否为空.正如 onCreate 所述:
When onCreate (Bundle savedInstanceState)
is called, check that savedInstanceState
is not null. As stated by onCreate :
savedInstanceState 如果活动在之后被重新初始化之前被关闭,那么这个 Bundle 包含它最多的数据最近在 onSaveInstanceState(Bundle) 中提供.注意:否则它为空.
savedInstanceState If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.
另见 onSaveInstanceState()不是正常生命周期的一部分,而是在活动被终止之前调用.
See also onSaveInstanceState() which is not part of the normal lifecycle but is called before the activity is killed.
注意:如果用户执行 A ----> B ----> C --back--> B ----> C,第二次创建 C 时传递的包将为 null
,因为它是一个新的实例,与被杀死后重新创建的不一样.
Note : if the user does A ----> B ----> C --back--> B ----> C, the second time C is created the passed bundle will be null
, as it is a new instance, not the same recreated after being killed.
这篇关于Android-如何在恢复时检测到我的应用程序被 VM 杀死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!