Android編程之Activity中onDestroy()調(diào)用分析
本文分析了Android編程之Activity中onDestroy()調(diào)用方法。分享給大家供大家參考,具體如下:
剛剛一個(gè)BUG讓我發(fā)現(xiàn),如果 activity 實(shí)現(xiàn)了一個(gè)回調(diào)接口,然后使用 this 設(shè)置給需要回調(diào)接口的方法,這種應(yīng)用場(chǎng)景比較常見,最常見的就是實(shí)現(xiàn) onClickListener 接口,然后 findViewById().setOnClickListenr(this)
如果,這個(gè)回調(diào)接口設(shè)置到了一個(gè)靜態(tài)對(duì)象(單例模式),當(dāng) activity finish() 的時(shí)候(按返回鍵,回到桌面),則activity 不會(huì)被調(diào)用 onDestroy() ,原因可能是 activity 對(duì)象還在被引用!
此時(shí)你再點(diǎn)擊圖標(biāo)回到應(yīng)用,onCreate() 再次調(diào)用!
很明顯,如果你把資源釋放放在了 onDestroy() 里面,就會(huì)導(dǎo)致內(nèi)存泄露!
那有沒有解決辦法呢?有的
你可以在 onPause() 方法里面判斷 isFinishing() ,正常調(diào)用 finish() 后 activity 的回調(diào)過程是 onPause、onStop、onDestroy ,倘若出現(xiàn)上面的情況,只到 onPause!但是 isFinishing() 標(biāo)志還是為 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.
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Material Design系列之Behavior實(shí)現(xiàn)Android知乎首頁
- php、java、android、ios通用的3des方法(推薦)
- Android5.0中Material Design的新特性
- Android數(shù)據(jù)加密之Des加密詳解
- Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航
- Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄
- 詳解Android Material Design自定義動(dòng)畫的編寫
- 學(xué)習(xí)Android Material Design(RecyclerView代替ListView)
- android:descendantFocusability方法介紹
- Android數(shù)據(jù)加密之Des加密
相關(guān)文章
Android自定義View之組合控件實(shí)現(xiàn)類似電商app頂部欄
這篇文章主要為大家詳細(xì)介紹了Android自定義View之組合控件,實(shí)現(xiàn)類似電商app頂部欄的相關(guān)資料,具有參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05新浪微博第三方登錄界面上下拉伸圖片之第三方開源PullToZoomListViewEx(一)
PullZoomView要實(shí)現(xiàn)兩類,一類是典型的Android ListView,另外一類是Android 的scroll view。本文先介紹PullZoomView在ListView上的實(shí)現(xiàn):PullToZoomListViewEx2015-12-12Android用戶輸入自動(dòng)提示控件AutoCompleteTextView使用方法
這篇文章主要為大家詳細(xì)介紹了Android用戶輸入自動(dòng)提示控件AutoCompleteTextView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android 開發(fā)之Dialog中隱藏鍵盤的正確使用方法
這篇文章主要介紹了Android 開發(fā)之Dialog中隱藏鍵盤的正確使用方法的相關(guān)資料,主要說明Dialog 隱藏鍵盤的注意事項(xiàng),需要的朋友可以參考下2017-09-09Android開發(fā)之利用ListView動(dòng)態(tài)刷新某個(gè)Item
這篇文章主要介紹了Android開發(fā)之利用ListView動(dòng)態(tài)刷新某個(gè)Item的方法,文章給出了詳解的示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起跟著小編學(xué)習(xí)學(xué)習(xí)吧。2016-12-12Android應(yīng)用開發(fā)中View繪制的一些優(yōu)化點(diǎn)解析
這篇文章主要介紹了Android應(yīng)用開發(fā)中View繪制的一些優(yōu)化點(diǎn)解析,包括Layout布局和硬件加速等方面,需要的朋友可以參考下2016-03-03Android高性能日志寫入方案的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Android高性能日志寫入方案的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01