为什么重写Activity.onDestroy()如果它不能可靠地称为? [英] Why override Activity.onDestroy() if it isn't reliably called?
问题描述
我很困惑,为什么有人会永远覆盖 Activity.onDestroy()如果按
而不是的onPause()
的到文档:
在有些情况下,系统只会杀死活动的
托管过程中没有在它调用此方法(或任何其他人),
块引用>我看到很多code重写
的onDestroy()
尽管此警告。为什么呢?解决方案
为什么要重写Activity.onDestroy()如果它不可靠叫什么名字?
块引用>这不是,这不是的可靠的叫......它只是它不是
活动
可以被杀死的唯一途径。 Android系统可能垃圾桶你的整个过程中没有给予ActivityManager
来调用的机会的onDestroy()
如果您的设备开始缺乏内存资源。由于这个原因,你不应该永远依靠
的onDestroy()
被调用,你应该一直留有在的onPause 持久状态code>。
I'm confused why anyone would ever override
Activity.onDestroy()
instead ofonPause()
if according to the documentation:There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it,
I see much code that overrides
onDestroy()
despite this warning. Why?解决方案Why override Activity.onDestroy() if it isn't reliably called?
It's not that it isn't reliably called... it's just that it isn't the only way the
Activity
can be killed. The Android system might trash your entire process without giving theActivityManager
the chance to callonDestroy()
if your device begins to lack memory resources.For this reason, you shouldn't ever rely on
onDestroy()
being called, and you should always save persistent state inonPause
.这篇关于为什么重写Activity.onDestroy()如果它不能可靠地称为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!