Activity生命周期
每一個Android應用程序在運行時,對於底層的Linux Kernel而言都是一個單獨的進程,但是對於Android系統而言,因為局限於手機畫面的大小與使用的考慮,不能把每一個運行中的應用程序窗口都顯示出來。
所以通常手機系統的界面一次僅顯示一個應用程序窗口,Android使用了Activity的概念來表示界面。
運行中的應用程序分為五大類,分別是:
前景模式:foreground process
可見模式:visible process
背景模式:background process
空白模式:empty process
服務模式:service process
除了最后一個,貌似service process是Service的事情了。其他都與Activity相關。
Android系統會判斷應用程序Activity是屬於哪一個類,給予不同的Activity生命周期。
Activity的生命周期也是它所在進程的生命周期。
Activity生命周期的運行如圖:
Activity生命周期進程類型
在Android系統中,即使不關掉程序,當應用程序處於某種進程類時,也有可能被系統kill掉。
Android系統通過運行機制,依照哪些畫面或消息對使用者最重要以及當前內存使用狀況,而做出是否kill Activity的決定。
foreground process(前景模式)是當前顯示於手機屏幕上的應用程序畫面,被定義為前景模式的進程,其中由onCreate()、onStart() 、onResume() 函數調用的Activity都會變成foreground process正在運行的Activity。
visible process(可見模式):visible process最常發生的情況是當應用程序彈出對話框要與用戶交互時,原應用程序就會變成透明(不可見)的,而對話窗口就會變成前景。
當對話窗口退出后,原應用程序馬上就又變回原前景模式了。
在Activity窗口畫面變為透明時,就會由onPause()函數掌控進入暫停狀態。
當前景進程退出時,該Activity就會再度被拉到前景模式,由onResume() 函數喚醒。
background process是在Activity窗口畫面被其他Activity完全蓋掉,窗口畫面已經完全看不見時,則會進入onStop()停止狀態。
這種情況通常發生在兩個不同的應用程序開啟時,后開啟的應用程序會覆蓋掉原應用程序。
此時對background process Activity的處理有兩種選擇:一是直接被onDestroy()退出,該程序將完全關閉,無法再使用任何返回鍵回到該程序;另一個處理方式是當其他Activity需要內存時,這個background process會先被清除掉,釋放出內存。
如果使用者再度瀏覽剛剛被清除掉的background process,則Android系統會自動再運行onCreate()重新啟動該Activity,所以當系統需要內存時,就會暫時將背景進程清除,讓它變成empty process(空白模式) , 所以空白進程最重要的目的就是暫時釋放出內存,直到使用者再度喚醒該empty process Activity時,才會將空白進程變成前景進程。
(Service相關)service process(服務模式進程)是由startService()所產生的,雖然服務進程有點類似背景進程在背景狀態運行,但是它的運行等級和前景進程幾乎一樣高。
服務模式進程是持續運行的,雖然使用者看不到任何運行畫面,Android系統不會自動關閉此類的服務進程,除非使用者自行關閉。這部分內容可在Service詳解里面再討論。
金字塔型的生命周期
Activity生命周期的每一個階段都表示為金字塔上的一個台階,當系統創建一個新的activity時,每一個回調函數都把activity的狀態網上挪一步。
金子塔的最頂層就是activity運行在前景模式下,用戶可與之交互。
當用戶離開activity時,系統調用另一些回調函數,將activity的狀態從金字塔中一步一步移下來。有些情況下,activity只移動一部分,並沒有完全到底,這些情況下仍然可以移動回頂部。
注意這些狀態中只有三個狀態是靜態(static)的,意味着activity只有在這三個狀態下能停留一段時間:
Resumed:foreground,用戶可交互running state
Paused:部分被遮擋,不能接收用戶輸入也不能執行代碼,另一個半透明或者小的activity正擋在前面。
Stopped:activity完全被遮擋,不能被用戶看到,activity被認為在background,當Stopped的時候,activity實例的狀態信息被保留,但是不能執行任何代碼。
其他狀態都是轉換狀態,系統會很快調用其他相應的回調函數離開這些狀態。比如系統調用onCreate()之后,會很快調用onStart(),之后是 onResume()。
回調函數
覆寫這些回調函數時,首先要記得一定要調用基類的回調函數,即最開始一行永遠是super.onXXX();
onPause()和onResume()中的動作應該互逆,比如說onPause()中釋放了相機,那么onResume()中就要重新初始化相機。
Stopped狀態下,UI對用戶完全不可見,此時用戶焦點在另一個activity或者另一個程序上。
onStop()中需要釋放資源,因為有時候系統會kill掉Stopped狀態的進程,如果有資源沒有被釋放,會造成內存泄露。
onStop()中還應該包括一些關閉操作,比如向數據庫寫信息。
當從Stopped狀態回到前景時,首先需要調用onRestart(),這個函數做一些恢復工作,恢復停止但是並沒有被銷毀的activity;然后系統會接着調用onStart(),因為每次activity變為可見時都要調用onStart()。
可以把onStart()和onStop()看成一對,因為在一開始啟動時和重新啟動時都需要做一些初始化工作。
onDestroy()一般都是在onPause()和onStop()之后調用,但有一個例外的情況:如果在onCreate()中調用finish()方法,系統將會立即調用onDestroy()而不用經過生命周期中的其他階段。
重新創建Activity
如果activity是自己銷毀的,實例就永遠消失了,但是如果系統因為資源限制銷毀了activity,雖然這個實例已經不在了,但是當用戶返回到它時,系統會利用這個activity被銷毀時存儲的數據,重新創建一個實例。(這個有點說來話長,且聽下回分解吧。)
重建Activity的詳細解釋:http://www.cnblogs.com/mengdd/archive/2012/12/17/2822291.html
Activity生命周期測試程序
注釋部分來自於《Professional Android 4 Application Development》:

import android.app.Activity; import android.os.Bundle; import android.util.Log; public class LifeOfActivity extends Activity { private static final String TAG = "ActivityLife"; // Called at the start of the full lifetime. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_life_of); Log.e(TAG, "start onCreate~~~"); // Initialize Activity and inflate the UI. } // Called after onCreate has finished, use to restore UI state. @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.e(TAG, "start onRestoreInstanceState~~~"); // Restore UI state from the savedInstanceState. // This Bundle has also been passed to onCreate. // Will only be called if the Activity has been killed by the system // since it was last visible. } // Called at the start of the visible lifetime. @Override protected void onStart() { super.onStart(); Log.e(TAG, "start onStart~~~"); // Apply any required UI change now that the Activity is visible. } // Called before subsequent visible lifetimes for an Activity process. @Override protected void onRestart() { super.onRestart(); Log.e(TAG, "start onRestart~~~"); // Load changes knowing that the Activity has already been visible // within this process. } // Called at the start of the active lifetime @Override protected void onResume() { super.onResume(); Log.e(TAG, "start onResume~~~"); // Resume any paused UI updates, threads, or processes required // by the Activity but suspended when it was inactive. } // Called to save UI state changes at the end of the active lifecycle. @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.e(TAG, "start onSaveInstanceState~~~"); // Save UI state changes to the outState(savedInstanceState). // This Bundle will be passed to onCreate and onRestoreInstanceState if // the process is killed and restarted by the run time. } // Called at the end of the active lifetime. @Override protected void onPause() { super.onPause(); Log.e(TAG, "start onPause~~~"); // Suspend UI updates, threads, or CPU intensive processes that don't // need to be updated when the Activity isn't the active foreground // Activity. } // Called at the end of the visible lifetime. @Override protected void onStop() { super.onStop(); Log.e(TAG, "start onStop~~~"); // Suspend remaining UI updates, threads, or processing that aren't // required when the Activity isn't visible. // Persist all edits or state changes as after this call the process if // likely to be killed. } // Sometimes called at the end of the full lifetime. @Override protected void onDestroy() { super.onDestroy(); Log.e(TAG, "start onDestroy~~~"); // Clean up any resources including ending threads, closing databases // connections etc. } }
參考資料
官網參考鏈接:
http://developer.android.com/reference/android/app/Activity.html
http://developer.android.com/training/basics/activity-lifecycle/starting.html
http://developer.android.com/training/basics/activity-lifecycle/pausing.html
http://developer.android.com/training/basics/activity-lifecycle/stopping.html
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
http://developer.android.com/guide/components/activities.html
兩分鍾讓你明白Acitivity生命周期:
http://www.apkbus.com/blog-99192-39550.html
四大組件:
http://www.apkbus.com/android-18204-1-1.html