Android編程之Activity中onDestroy()調用分析
本文分析了Android編程之Activity中onDestroy()調用方法。分享給大家供大家參考,具體如下:
剛剛一個BUG讓我發(fā)現(xiàn),如果 activity 實現(xiàn)了一個回調接口,然后使用 this 設置給需要回調接口的方法,這種應用場景比較常見,最常見的就是實現(xiàn) onClickListener 接口,然后 findViewById().setOnClickListenr(this)
如果,這個回調接口設置到了一個靜態(tài)對象(單例模式),當 activity finish() 的時候(按返回鍵,回到桌面),則activity 不會被調用 onDestroy() ,原因可能是 activity 對象還在被引用!
此時你再點擊圖標回到應用,onCreate() 再次調用!
很明顯,如果你把資源釋放放在了 onDestroy() 里面,就會導致內存泄露!
那有沒有解決辦法呢?有的
你可以在 onPause() 方法里面判斷 isFinishing() ,正常調用 finish() 后 activity 的回調過程是 onPause、onStop、onDestroy ,倘若出現(xiàn)上面的情況,只到 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程序設計有所幫助。
- Material Design系列之Behavior實現(xiàn)Android知乎首頁
- php、java、android、ios通用的3des方法(推薦)
- Android5.0中Material Design的新特性
- Android數(shù)據(jù)加密之Des加密詳解
- Android程序開發(fā)之使用Design包實現(xiàn)QQ動畫側滑效果和滑動菜單導航
- Android App仿QQ制作Material Design風格沉浸式狀態(tài)欄
- 詳解Android Material Design自定義動畫的編寫
- 學習Android Material Design(RecyclerView代替ListView)
- android:descendantFocusability方法介紹
- Android數(shù)據(jù)加密之Des加密
相關文章
android 限制某個操作每天只能操作指定的次數(shù)(示例代碼詳解)
這篇文章主要介紹了android 限制某個操作每天只能操作指定的次數(shù),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Ubuntu中為Android增加硬件抽象層(HAL)模塊訪問Linux內核驅動程序
本文主要介紹在Ubuntu上為Android HAL模塊訪問Linux內核驅動程序,這里給大家提供方法和一個小的測試程序代碼,以及常遇到的問題和解決方法,有需要的小伙伴可以參考下2016-08-08
設置界面開發(fā)Preference Library數(shù)據(jù)重建機制詳解
這篇文章主要為大家介紹了設置界面開發(fā)利器Preference Library數(shù)據(jù)重建機制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Android 使用PopupWindow實現(xiàn)彈出更多的菜單實例詳解
最近想要做一個彈出更多的菜單,而原生的彈出菜單卻不是我們想要的效果,所以必然要自定義菜單。接下來通過本文給大家介紹android 使用popupwindow實現(xiàn)彈出更多的菜單實例詳解,需要的朋友可以參考下2017-04-04
Android自定義view利用Xfermode實現(xiàn)動態(tài)文字加載動畫
這篇文章主要介紹了Android自定義view利用Xfermode實現(xiàn)動態(tài)文字加載動畫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

