Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

深入透徹理解 Android Activity 生命週期

編輯:關於 Android 程式設計

今天為大家詳細拆解 Android 中 Activity 的生命週期。我希望自己的講解不同於網上那些多半只是翻譯官方 API、過於籠統的文章——相信你讀完會有所收穫,但未必能完全吃透,所以我特意重新梳理一遍,力求讓你真正掌握。

先來看 Android API 提供的 Activity 生命週期圖(若一時看不明白,不妨先讀完整篇,再回頭對照,你會豁然開朗):

Activity 其實繼承自 ApplicationContext,我們可以重寫以下方法:

public class Activity extends ApplicationContext {
    protected void onCreate(Bundle savedInstanceState);
    protected void onStart();
    protected void onRestart();
    protected void onResume();
    protected void onPause();
    protected void onStop();
    protected void onDestroy();
}

為便於理解,我寫了一個 Demo,大家可以親手實踐。跟著步驟來:

第一步:新建一個 Android 工程,我這裡命名為 ActivityDemo。

第二步:修改 ActivityDemo.java,重寫以上七種方法,主要用 Log 列印:

package com.tutor.activitydemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class ActivityDemo extends Activity {
    private static final String TAG = "ActivityDemo";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.e(TAG, "start onCreate~~~");
    }
    @Override protected void onStart() { super.onStart(); Log.e(TAG, "start onStart~~~"); }
    @Override protected void onRestart() { super.onRestart(); Log.e(TAG, "start onRestart~~~"); }
    @Override protected void onResume() { super.onResume(); Log.e(TAG, "start onResume~~~"); }
    @Override protected void onPause() { super.onPause(); Log.e(TAG, "start onPause~~~"); }
    @Override protected void onStop() { super.onStop(); Log.e(TAG, "start onStop~~~"); }
    @Override protected void onDestroy() { super.onDestroy(); Log.e(TAG, "start onDestroy~~~"); }
}

第三步:運行工程,效果圖如下(沒什麼特別):

核心在 Logcat 視窗。當我們打開應用時,先後執行了 onCreate()→onStart()→onResume() 三個方法,LogCat 如下:

按 BACK 鍵:當我們按下 BACK 鍵,應用結束,此時先後呼叫 onPause()→onStop()→onDestroy() 三個方法:

按 HOME 鍵:比如你正在用瀏覽器看 NBA 新聞,看到一半突然想聽歌,於是按下 HOME 鍵去打開音樂應用。此時 Activity 先後執行 onPause()→onStop(),但應用並未銷毀:

而當我們再次啟動 ActivityDemo 時,則先後執行 onRestart()→onStart()→onResume() 三個方法:

這裡引出一個問題:按 HOME 鍵後再進入應用,理論上狀態應與按 HOME 之前一致。為方便理解,我修改一下程式碼,增加一個 EditText。

第四步:修改 main.xml 佈局檔案(增加一個 EditText):

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"/>
    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

第五步:其他不變,運行程序,在 EditText 裡輸入 "Frankie":

這時按下 HOME 鍵,再次啟動應用,你會發現 EditText 裡並沒有我們輸入的 "Frankie":

這顯然不是一個合格的應用。所以我們需要在 Activity 的幾個方法裡自行實現狀態保存:

第六步:修改 ActivityDemo.java:

package com.tutor.activitydemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
public class ActivityDemo extends Activity {
    private static final String TAG = "ActivityDemo";
    private EditText mEditText;
    private String mString; // 用來暫存 EditText 輸入的值
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mEditText = (EditText) findViewById(R.id.editText);
        Log.e(TAG, "start onCreate~~~");
    }
    @Override protected void onStart() { super.onStart(); Log.e(TAG, "start onStart~~~"); }
    // 按 HOME 後再次啟動時,恢復先前狀態
    @Override protected void onRestart() {
        super.onRestart();
        mEditText.setText(mString);
        Log.e(TAG, "start onRestart~~~");
    }
    @Override protected void onResume() { super.onResume(); Log.e(TAG, "start onResume~~~"); }
    // 按 HOME 時,在 onPause 中保存輸入的值
    @Override protected void onPause() {
        super.onPause();
        mString = mEditText.getText().toString();
        Log.e(TAG, "start onPause~~~");
    }
    @Override protected void onStop() { super.onStop(); Log.e(TAG, "start onStop~~~"); }
    @Override protected void onDestroy() { super.onDestroy(); Log.e(TAG, "start onDestroy~~~"); }
}

第七步:重新運行,重複第五步操作。當我們按下 HOME 鍵後再次啟動應用時,EditText 裡便保留了上次輸入的 "Frankie":

大功告成!此時再回頭對照最開始的 Activity 生命週期圖,相信你已經完全理解了 Activity 的生命週期。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved