
- 關(guān)于是否一定要實現(xiàn)onDestory()方法,官方給出下面的說法
Most apps don't need to implement this method because local class references are destroyed with the activity and your activity should perform most cleanup during onPause() and onStop(). However, if your activity includes background threads that you created during onCreate() or other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy()
意思就是說: 當(dāng)我們在onCreate方法中執(zhí)行了一些耗時的操作,例如線程或者其他需要內(nèi)存開銷很大而又不能釋放的,就必須在onDestory中釋放.
- 關(guān)于在onPause()方法中可以執(zhí)行的一些操作和注意事項
2.1 Stop animations or other ongoing actions that could consume CPU.
停止一些動畫或者其他可以釋放CPU正在進(jìn)行的操作
2.2 Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
提交無法保存的變化,但是是只有當(dāng)用戶希望保存在離開之前.
2.3 Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them
釋放系統(tǒng)資源,例如廣播.或者其他影響到電池生命的資源以及用戶不需要的
2.4 Generally, you should not use onPause() to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage within onPause() is when you're certain users expect the changes to be auto-saved (such as when drafting an email). However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop()
簡單的意思就是說: 不應(yīng)該去存儲一些用戶的信息,除非是自動保存的.并且應(yīng)該避免一些耗時的CPU操作和讀取數(shù)據(jù)庫的操作,因為會造成下一個Acitivty出現(xiàn)延遲
- onStop()方法和onRestart()方法
3.1 In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.
當(dāng)系統(tǒng)內(nèi)存不足的時候,系統(tǒng)就會直接殺死你的程序而不通過調(diào)用onDestory()方法,所以你應(yīng)該在onStop()方法中去釋放資源
在onResume()方法和onPause()方法中,最好不要做太多的耗時操作,導(dǎo)致UI加載過慢才出來.影響用戶體驗.
關(guān)于onSaveInstanceState()方法: 當(dāng)用戶不覆蓋該方法,系統(tǒng)也會自動去保存有設(shè)置唯一ID值的控件