編輯:關於 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 的生命週期。
用 Bitmap 把長方形圖片裁成正圓形的兩種方法
想做出正圓形的頭像,前提是從正方形裁切——直接在長方形上畫圓只會得到橢圓。所以處理思路是:先以短邊為基準把圖片裁成正方形,再畫圓。 原圖: 處理後: 方法一:自動居中
用 Espresso、Mockito 與 Dagger 2 打造快速可靠的 Android UI 測試
前言 Iván Carballo 與他的團隊寫了大約 250 個 UI 測試,跑完只要三分鐘。這篇整理他們的做法:如何用 Mockito 與 Dagger 2 把 UI
Android 中 Context 的應用總結與內存洩漏防範
概述 作為一名 Android 開發人員,每天使用最多的類中,恐怕 Context 可以排得上號了。因為 Context 對象在我們的項目裏實在是太常見了:加載資源、啟動
Android 自定義 DragGridView:長按拖拽與數據交換
在一些需要自定義排序的頁面中,用戶可能希望通過長按拖拽來調整 item 順序。實現這種效果,可以基於 GridView 進行擴展,結合長按監聽、WindowManager