当前位置: 首页 > 编程笔记 >

Android编程之Activity中onDestroy()调用分析

邓光耀
2023-03-14
本文向大家介绍Android编程之Activity中onDestroy()调用分析,包括了Android编程之Activity中onDestroy()调用分析的使用技巧和注意事项,需要的朋友参考一下

本文分析了Android编程之Activity中onDestroy()调用方法。分享给大家供大家参考,具体如下:

刚刚一个BUG让我发现,如果 activity 实现了一个回调接口,然后使用 this 设置给需要回调接口的方法,这种应用场景比较常见,最常见的就是实现 onClickListener 接口,然后 findViewById().setOnClickListenr(this)

如果,这个回调接口设置到了一个静态对象(html" target="_blank">单例模式),当 activity finish() 的时候(按返回键,回到桌面),则activity 不会被调用 onDestroy() ,原因可能是 activity 对象还在被引用!

此时你再点击图标回到应用,onCreate() 再次调用!

很明显,如果你把资源释放放在了 onDestroy() 里面,就会导致内存泄露!

那有没有解决办法呢?有的

你可以在 onPause() 方法里面判断 isFinishing() ,正常调用 finish() 后 activity 的回调过程是 onPause、onStop、onDestroy ,倘若出现上面的情况,只到 onPause!但是 isFinishing() 标志还是为 true !你可以释放资源了。

我们来看下  onDestroy 的官方解释:

protected void onDestroy () 
Added in API level 1 
Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. 
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. 
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown. 

希望本文所述对大家Android程序设计有所帮助。

 类似资料:
  • 本文向大家介绍Android编程之四种Activity加载模式分析,包括了Android编程之四种Activity加载模式分析的使用技巧和注意事项,需要的朋友参考一下 本文分析讲述了Android编程之四种Activity加载模式。分享给大家供大家参考,具体如下: Activity状态 一般认为Activity有以下四种状态: ① 活动的:当一个Activity在栈顶,它是可视的、有焦点、可接受用

  • 本文向大家介绍Android编程之listView中checkbox用法实例分析,包括了Android编程之listView中checkbox用法实例分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程之listView中checkbox用法。分享给大家供大家参考,具体如下: 我们经常会用到在listView中使用checkbox的情况。直接不回应用后会发现,ListVi

  • 本文向大家介绍Android编程中Activity的四种启动模式,包括了Android编程中Activity的四种启动模式的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程中Activity的四种启动模式。分享给大家供大家参考,具体如下: Activity启动方式有四种,分别是: standard singleTop singleTask singleInstance 可以

  • 本文向大家介绍Android编程之TabWidget选项卡用法实例分析,包括了Android编程之TabWidget选项卡用法实例分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程之TabWidget选项卡用法。分享给大家供大家参考,具体如下: 1 概览 TabWidget与TabHost。tab组件一般包括TabHost和TabWidget、FrameLayout,且

  • 问题内容: 一个简单的问题:您确定可以致电吗?我没有找到任何确认。 问题答案: 一个简单的问题:您可以确定finish()将调用onDestroy()吗? 首先,此答案假定您正在引用Android的类及其方法和生命周期方法。 其次,这取决于您对“确定”的定义: 您的流程可能在和之间终止,原因与触发调用的原因无关 设备制造商或ROM修改器可能会引入一些螺钉更改,从而破坏与 电池可能在和之间耗尽 等等

  • 我在活动的中放置了一些缓存清理代码,但大多数情况下,除非通过显式地完成活动,否则这些代码不会执行。 编辑:只读仅在或系统资源不足时调用。那么我需要把我的缓存清理代码放在哪里呢?如果我把它放在中,用户返回到应用程序,缓存就会被清除。我实际上是在缓存中存储重要的临时文件,这些文件不应该在中删除。