大家好,今天給大家詳解一下Android中Activity的生命周期,我在前面也曾經講過這方面的内容,但是像網上大多數文章一樣,基本都是翻譯Android API,過于籠統,相信大家看了,會有一點點的幫助 ,但是還不能完全吃透,是以我今天特意在重新總結一下.
首先看一下Android api中所提供的Activity生命周期圖(不明白的,可以看完整篇文章,在回頭看一下這個圖,你會明白的):
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5SM3EHOxADMxIzMwgjMx8FMvwFOy8CX3ADMxAjMvw1ckF2bsBXdvwFdl5mLuR2cj5Set1yZtl2Lc9CX6MHc0RHaiojIsJye.gif)
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();
}
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,不明白Activity周期的朋友們,可以親手實踐一下,大家按照我的步驟來。
第一步:建立一個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~~~");
protected void onRestart() {
super.onRestart();
Log.e(TAG, "start onRestart~~~");
protected void onResume() {
super.onResume();
Log.e(TAG, "start onResume~~~");
protected void onPause() {
super.onPause();
Log.e(TAG, "start onPause~~~");
protected void onStop() {
super.onStop();
Log.e(TAG, "start onStop~~~");
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "start onDestroy~~~");
}
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~~~");
protected void onRestart() {
super.onRestart();
Log.e(TAG, "start onRestart~~~");
protected void onResume() {
super.onResume();
Log.e(TAG, "start onResume~~~");
protected void onPause() {
super.onPause();
Log.e(TAG, "start onPause~~~");
protected void onStop() {
super.onStop();
Log.e(TAG, "start onStop~~~");
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "start onDestroy~~~");
第三步:運作上述工程,效果圖如下(沒什麼特别的):
BACK鍵:
當我們按BACK鍵時,我們這個應用程式将結束,這時候我們将先後調用onPause()->onStop()->onDestory()三個方法,如下圖所示:
HOME鍵:
當我們打開應用程式時,比如浏覽器,我正在浏覽NBA新聞,看到一半時,我突然想聽歌,這時候我們會選擇按HOME鍵,然後去打開音樂應用程式,而當我們按HOME的時候,Activity先後執行了onPause()->onStop()這兩個方法,這時候應用程式并沒有銷毀。如下圖所示:
而當我們再次啟動ActivityDemo應用程式時,則先後分别執行了onRestart()->onStart()->onResume()三個方法,如下圖所示:
這裡我們會引出一個問題,當我們按HOME鍵,然後再進入ActivityDemo應用時,我們的應用的狀态應該是和按HOME鍵之前的狀态是一樣的,同樣為了友善了解,在這裡我将ActivityDemo的代碼作一些修改,就是增加一個EditText。
第四步:修改main.xml布局檔案(增加了一個EditText),代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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_height="wrap_content"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/editText"
</LinearLayout>
第五步:然後其他不變,運作ActivityDemo程式,在EditText裡輸入如"Frankie"字元串(如下圖:)
這時候,大家可以按一下HOME鍵,然後再次啟動ActivityDemo應用程式,這時候EditText裡并沒有我們輸入的"Frankie"字樣,如下圖:
這顯然不能稱得一個合格的應用程式,是以我們需要在Activity幾個方法裡自己實作,如下第六步所示:
第六步修改ActivityDemo.java代碼如下:
import android.widget.EditText;
private EditText mEditText;
//定義一個String 類型用來存取我們EditText輸入的值
private String mString;
mEditText = (EditText)findViewById(R.id.editText);
//當按HOME鍵時,然後再次啟動應用時,我們要恢複先前狀态
mEditText.setText(mString);
//當我們按HOME鍵時,我在onPause方法裡,将輸入的值賦給mString
mString = mEditText.getText().toString();
import android.widget.EditText;
private EditText mEditText;
//定義一個String 類型用來存取我們EditText輸入的值
private String mString;
mEditText = (EditText)findViewById(R.id.editText);
//當按HOME鍵時,然後再次啟動應用時,我們要恢複先前狀态
mEditText.setText(mString);
//當我們按HOME鍵時,我在onPause方法裡,将輸入的值賦給mString
mString = mEditText.getText().toString();
第七步:重新運作ActivityDemo程式,重複第五步操作,當我們按HOME鍵時,再次啟動應用程式時,EditText裡有上次輸入的"Frankie"字樣,如下圖如示:
OK,大功基本告成,這時候大家可以在回上面看一下Activity生命周期圖,我想大家應該完全了解了Activity的生命周期了,不知道你了解了沒?