Source: http://www.cnblogs.com/everhad/p/6360415.html
生命周期圖
新的Activity啟動并顯示,以及隱藏或結束時其生命周期回調的執行順序如下:
金字塔階梯式調用
在不同狀態間轉變時,生命周期方法的調用都會按照圖中的順序,不會跳躍;
比如從stopped進入Resumed時:onStart()->onResume();
Activity的狀態
Activity運行期間,當它在不同生命周期階段間切換時執行不同的生命周期回調。
只有三個生命周期階段可以長期處于,其它都是臨時的。
Resumed
In this state, the activity is in the foreground and the user can interact with it. (Also sometimes referred to as the "running" state.)Paused
In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code.Stopped
In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.
onCreate()和onDestroy()
onCreate()
onCreate()僅執行一次,在這里定義ui,執行初始化。
onCreate()執行結束后,系統依次快速地執行onStart()和onResume(),Activity不會處于Created和Started狀態。
從技術上講,onStart()執行后界面已經用戶可見了,但是onResume()很快接著就執行了,之后立即進入Started狀態。直到以后當鎖屏、來電界面、切換到其它Activity等發生才更變為其它狀態。
onDestroy()
最后被執行,之后此Activity對象不再被使用。這里執行一些資源釋放等清理操作。
如果在onCreate()中執行了finish()那么系統直接執行onDestroy()而沒有其它任何回調被執行。
onResume()和onPaused()
onPaused()
當Activity由完全可見,可獲得焦點的狀態向不完全可見,不可獲得焦點轉變時,onPaused()被執行。例如對話框style的其它Activity遮蓋當前界面等。
onPaused()執行后,若Activity繼續處于部分可見,不可獲得焦點,它就處在了Paused狀態。
若處在Paused時,界面回到完全可見,可獲得焦點,那么onResume()執行,進入Resumed狀態。
若用戶正在離開當前界面,那么界面直接完全不可見,那么就會快速依次執行onPaused()、onStop()。
onPaused()中不應該執行耗時操作,如寫數據庫,避免阻塞其它界面的顯示。
可以停止一些與焦點、可見狀態有關的操作,如動畫。
Paused狀態下Activity的狀態也不會丟失,就像Resumed狀態那樣。
onResume()
啟動Activity時,onResume()執行。
向Resumed狀態轉變時,onResume()執行。
因為會多次執行,所以它應該配合onPaused(),做一些焦點、界面可見性相關的操作的啟動。
Stop and Restart
用戶打開其它Activity,或其它App,當前界面完全不可見——進入后臺。
onStop()執行,進入Stopped狀態,此時系統保持Activity的對象狀態,所以在達到Resumed時所做的狀態設置都會得到保持,包括View狀態,如EditText。
稍后用戶返回當前界面時,onStart()執行。
onRestart()
若Activity是從Stopped重新向Resumed狀態轉變——沒有在Stopped時被回收,那么onRestart()執行。
onStop()
在臨時進入后臺,或finishing過程中,都會執行onStop(),所以它的執行意味著用戶很可能不再返回當前Activity了——finishing,或者返回前系統已經殺死當前進程了——系統內容回收時殺死后臺進程,此時onStop()后不執行onDestroy()!
因此onStop()中應該執行一些釋放資源的操作,它比onPause()更適合執行一些耗時操作。
create和finish
create時依次執行:onCreate、onStart、onResume;
finish時依次執行:onPause、onStop、onDestroy;
Recreating an Activity
App處于后臺狀態時,系統可能回收進程,那么Activity可能在Stopped狀態被殺死。
之后用戶回到當前界面時,對應Activity會重新創建。
如果界面是轉屏的,屏幕旋轉時Activity也會先銷毀后重建。
可以在:protected void onSaveInstanceState (Bundle outState)
中保存狀態以便重建的Activity可以恢復。
onCreate(Bundle savedInstanceState)參數savedInstanceState中即可以獲得之前保存的數據。
也可以在public void onRestoreInstanceState(Bundle savedInstanceState)
中恢復狀態,它在onStart()之后執行。
系統會自動保存界面的view hierarchy的狀態,如ListView的滾動位置,EditText的輸入狀態。不過需要view被設置了id屬性。
onSaveInstanceState()和onRestoreInstanceState()中記得調用super的方法,以便系統恢復view hierarchy的狀態。
啟動其它Activity時
啟動其它Activity時,newActivity的啟動和當前Activity進入Stopped狀態的過程是交叉的。
所以如果需要在oldActivity和newActivity的停止、啟動過程中做一些相互沖突的工作,那么:
簡單的依賴生命周期回調順序+同步是沒意義的,這些方法都是在主線程中執行的。
可以使用“設置者標記”法:
不是你設置的,就不要去清除標記。
一般類似:
if (mCurrentXxx == this) {
mCurrentXxx = null;
}
補充
onResume和onPause
配合開啟和關閉一些與可見性、焦點相關的操作,必須是快速完成的輕量操作。
可能反復執行。onStart和onStop
配合開啟和關閉一些與Activity進入前臺、后臺相關的操作,可以是耗時操作。
可能反復執行。onStop和onDestroy
應該在onStop中做許多操作的停止操作。而onDestroy中做一些最后的清理操作,避免內存泄露。
onDestroy在后臺進程殺死時并不一定執行,而進入后臺時onStop一定會執行,但onStop又可能反復執行。所以系統資源的釋放盡量在onStop中,這樣確保一定可以釋放。
(本文使用Atom編寫)
![]() |
不含病毒。www.avast.com |