为什么重写Activity.onDestroy()如果​​它不能可靠地称为? [英] Why override Activity.onDestroy() if it isn't reliably called?

查看:437
本文介绍了为什么重写Activity.onDestroy()如果​​它不能可靠地称为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑,为什么有人会永远覆盖 Activity.onDestroy()如果​​按而不是的onPause()的到文档


  

在有些情况下,系统只会杀死活动的
  托管过程中没有在它调用此方法(或任何其他人),


我看到很多code重写的onDestroy()尽管此警告。为什么呢?


解决方案

  

为什么要重写Activity.onDestroy()如果​​它不可靠叫什么名字?


这不是,这不是的可靠的叫......它只是它不是活动可以被杀死的唯一途径。 Android系统可能垃圾桶你的整个过程中没有给予 ActivityManager 来调用的机会的onDestroy()如果您的设备开始缺乏内存资源。

由于这个原因,你不应该永远依靠的onDestroy()被调用,你应该一直留有在的onPause

I'm confused why anyone would ever override Activity.onDestroy() instead of onPause() 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 the ActivityManager the chance to call onDestroy() 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 in onPause.

这篇关于为什么重写Activity.onDestroy()如果​​它不能可靠地称为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆