編輯:關於Android編程
本文分析了Android編程之Activity中onDestroy()調用方法。分享給大家供大家參考,具體如下:
剛剛一個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 的官方解釋:
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程序設計有所幫助。
細心的小伙伴可能都現了微信下拉沒有小視頻功能只顯示微信圖標,最新版本的微信下拉不能拍小視頻啦,這是什麼原因呢?如果你還在疑惑微信下拉小視頻怎麼沒有了這個問題
對之前的幾篇文章裡的model進行補充後期會把這個功能類,添加到這個框架裡,有興趣的可以下載下來看,這個框架會經常更新:public class BaseFundChar
handler在Android中被稱為“消息處理者”,在多線程中比較常用。Handler為Android提供了一種異步消息處理機制,當向消息隊列中發送消息 (sendMe
一、 問題描述 用戶首次後再次進入小程序時,我們通常需要通過獲取用戶openid或unionid用作唯一標示與後台進行數據交流,初始化用戶信息。當我們通過第三方服務器跟微