Activity onDestroy() 调用研究 内存泄漏

来源:互联网 发布:图像识别算法工程师 编辑:程序博客网 时间:2024/06/15 14:35



http://blog.csdn.net/z1074971432/article/details/10517449

http://blog.csdn.net/z1074971432/article/details/10517449

http://blog.csdn.net/z1074971432/article/details/10517449

http://blog.csdn.net/z1074971432/article/details/10517449

http://blog.csdn.net/z1074971432/article/details/10517449




 

Activity onDestroy() 调用研究

标签: androidactivity生命周期
 21976人阅读 评论(10) 收藏 举报

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


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

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

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


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

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


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

[html] view plain copy
  1. protected void onDestroy ()  
  2.   
  3. Added in API level 1  
  4. 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.  
  5.   
  6. 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.  
  7.   
  8. Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.  






http://blog.sina.com.cn/s/blog_95c607dd0102ux1v.html
http://blog.sina.com.cn/s/blog_95c607dd0102ux1v.html
http://blog.sina.com.cn/s/blog_95c607dd0102ux1v.html

Activity中finish()和onDestroy()的区别

 (2014-07-07 13:30:39)
转载
标签: 

it

分类: android

finish()方法用于结束一个Activity的生命周期,而onDestory()方法则是Activity的一个生命周期方法,其作用是在一个Activity对象被销毁之前,Android系统会调用该方法,用于释放此Activity之前所占用的资源。

finish会调用到onDestroy方法,
可以在onDestroy里打印一句话,就会发现在finish方法那也会打印这句话。。。

 

 

Activity.finish()
Call this when your activity is done and should be closed.
在你的activity动作完成的时候,或者Activity需要关闭的时候,调用此方法。
当你调用此方法的时候,系统只是将最上面的Activity移出了栈,并没有及时的调用onDestory()方法,其占用的资源也没有被及时释放。因为移出了栈,所以当你点击手机上面的“back”按键的时候,也不会再找到这个Activity。
Activity.onDestory()
the system is temporarily destroying this instance of the activity to save space.
系统销毁了这个Activity的实例在内存中占据的空间。
在Activity的生命周期中,onDestory()方法是他生命的最后一步,资源空间等就被回收了。
当重新进入此Activity的时候,必须重新创建,执行onCreate()方法。

 




0 0