android 监听器需不需要销毁,android – 在onDestroy中取消注册侦听器 – 它们有什么危害/可以泄漏?...

我试图了解在onDestroy取消注册监听器之前是否有任何潜在的危害.我特别指的是从事件总线(otto)取消订阅,但相信答案适用于大多数侦听器模式实现(SharedPreferenceListener,LocationListener,静态引用等).

我已经看到several other answers这个引用短语’onDestroy不能保证被调用’的问题.完整的onDestroy documentation文本实际上是:

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity’s hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

我理解这意味着onDestroy不应该用于保存数据,因为它可能在应用程序进程被杀死之前不被调用(如生命周期图所示).

简单地说,由于没有调用onDestroy,是否存在注册为侦听器的Activity可能被泄露的情况?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值