前言:
學習Android也有一年瞭,不敢說精通,但也小有心得。相信很多android初學者和我剛開始接觸android時一樣,往往隻瞭解某些知識點的表面,別人要是問點啥,還真就說不出個一二三來。所以,我打算將自己學習android的過程整理一下,發表出來,希望對學習android的同仁有所幫助,也算給自己的學習進行總結吧。在我的學習過程中,有一點經驗和大傢分享一下,那就是“Deep Learning”—深入學習。多花點時間深入學習,總比走馬觀花強得多。今天,就先說說Activity的生命周期吧,很多面試官都喜歡問這個問題。。。
Activity這個類,定義瞭一些回調函數來控制它的生命周期。
onCreate() —— 當Activity第一次創建的時候被調用。
onStart() —— 當Activity對用戶可見的時候被調用。
onResume() —— 當Activity開始和用戶交互的時候被調用。
onPause() —— 正在運行的Activity馬上要被暫停的時候被調用,此時,在這之前的Activity被重新獲取。
onStop() —— 當Activity不在對用戶可見的時候被調用。
默認地,被創建的Activity中都包含一個onCreate()方法,通過這個方法,可以創建顯示給用戶的UI組件。
從“被創建”到“被銷毀”的生命周期圖示:
想要理解Activity生命周期的最好辦法就是創建一個工程,並實現所有的回調函數,然後讓Activity與用戶交互。
1.創建一個名為Activity101的工程。
2.Activity101Activity.java中的代碼。
[java]
package net.horsttnann.Activity101;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Activity101Activity extends Activity {
String tag = "Lifecycle";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(tag, "In the onCreate() event");
}
public void onStart() {
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart() {
super.onRestart();
Log.d(tag, "In the onRestart() event");
}
public void onResume() {
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onPause() {
super.onPause();
Log.d(tag, "In the onPause() event");
}
public void onStop() {
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy() {
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
}
3.按F11在模擬器上調試。www.aiwalls.com
4.當這個activity第一次被加載:
[plain]
03-23 01:54:32.602: D/Lifecycle(644): In the onCreate() event
03-23 01:54:32.602: D/Lifecycle(644): In the onStart() event
03-23 01:54:32.602: D/Lifecycle(644): In the onResume() event
5.按“返回鍵”,程序退出:
[plain]
03-23 01:58:28.307: D/Lifecycle(644): In the onPause() event
03-23 01:58:28.762: D/Lifecycle(644): In the onStop() event
03-23 01:58:28.837: D/Lifecycle(644): In the onDestroy() event
03-23 01:58:28.307: D/Lifecycle(644): In the onPause() event
03-23 01:58:28.762: D/Lifecycle(644): In the onStop() event
03-23 01:58:28.837: D/Lifecycle(644): In the onDestroy() event
6.重新進入程序:
[plain]
03-23 01:59:38.282: D/Lifecycle(644): In the onCreate() event
03-23 01:59:38.292: D/Lifecycle(644): In the onStart() event
03-23 01:59:38.302: D/Lifecycle(644): In the onResume() event
03-23 01:59:38.282: D/Lifecycle(644): In the onCreate() event
03-23 01:59:38.292: D/Lifecycle(644): In the onStart() event
03-23 01:59:38.302: D/Lifecycle(644): In the onResume() event
7.按“撥號鍵”進入撥號界面,activity被轉入後臺運行:
[plain]
03-23 02:00:23.252: D/Lifecycle(644): In the onPause() event
03-23 02:00:24.522: D/Lifecycle(644): In the onStop() event
03-23 02:00:23.252: D/Lifecycle(644): In the onPause() event
03-23 02:00:24.522: D/Lifecycle(644): In the onStop() event
8.註意,此時onDestroy()方法並沒有被觸發,說明這個activity還在內存中。按“返回鍵”,退出撥號界面,這個Activity又重新可見瞭。觀察LogCat窗口中的輸出:
[plain]
03-23 02:03:25.262: D/Lifecycle(644): In the onRestart() event
03-23 02:03:25.262: D/Lifecycle(644): In the onStart() event
03-23 02:03:25.262: D/Lifecycle(644): In the onResume() event
03-23 02:03:25.262: D/Lifecycle(644): In the onRestart() event
03-23 02:03:25.262: D/Lifecycle(644): In the onStart() event
03-23 02:03:25.262: D/Lifecycle(644): In the onResume() event
onRestart()方法被觸發瞭,接下來是onStart()和onResume()。
可以從這個簡單的例子中看到,當點擊“返回鍵”的時候,activity被銷毀瞭,與此同時,activity當前的狀態也將消失。有一點需要特別註意,onPause()方法僅在兩種情況下被調用:一個是在Activity被轉入後臺運行的時候,一個是用戶按“返回鍵”將activity銷毀的時候。
當一個anctivity被啟動之後,onStart()和onResume()方法總是要被調用的,無論這個activity是從後臺重新獲取的,還是首次被創建的。當一個anctivity第一次被創建的時候,onCreate()方法總是被調用。
從上面的例子,我們可以得出結論:
使用onCreate()方法去創建和初始化將要使用的組件。
使用onResume()方法去開啟服務,執行那些需要在“activity前臺”運行的代碼。
使用onPause()方法去停止服務,執行那些需要在“activity後臺”運行的代碼。
使用onDestroy()方法去釋放資源。
摘自 horsttnann