Activity之生命周期

Activity 作為Android的四大組件之一,同時(shí)也是用戶唯一能看得見與之進(jìn)行交互的控件。所以熟悉Activity對(duì)于開發(fā)Android變得尤為重要,理解它的生命周期對(duì)于熟悉Activity有很大幫助。本節(jié)主要為大家講解 一下Activity的運(yùn)行的生命周期。

首先,大家可以先看一下Android API 中提供的Activity的生命周期圖:


為了方便大家理解,我們新建一個(gè)Android工程。

一、修改文件名為ActivityDemo.java,代碼如下:

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

protectedvoidonStart()?{

super.onStart();

Log.e(TAG,"start?onStart~~~");

}

@Override

protectedvoidonRestart()?{

super.onRestart();

Log.e(TAG,"start?onRestart~~~");

}

@Override

protectedvoidonResume()?{

super.onResume();

Log.e(TAG,"start?onResume~~~");

}

@Override

protectedvoidonPause()?{

super.onPause();

Log.e(TAG,"start?onPause~~~");

}

@Override

protectedvoidonStop()?{

super.onStop();

Log.e(TAG,"start?onStop~~~");

}

@Override

protectedvoidonDestroy()?{

super.onDestroy();

Log.e(TAG,"start?onDestroy~~~");

}

}

運(yùn)行上述工程,查看Logcat窗口,當(dāng)我們打開應(yīng)用是先后執(zhí)行了onCreate()->onStart()->onResume()方法,Logcat窗口如下:


按Back鍵:

當(dāng)我們按下Back鍵時(shí),應(yīng)用將結(jié)束運(yùn)行,這時(shí)先后調(diào)用了onPause()->onStop()->onDestory()方法,Logcat窗口如下:


按Home鍵:

當(dāng)我們打開應(yīng)用時(shí),此時(shí)想打開音樂播放器聽音樂,可能會(huì)選擇按Home鍵,然后打開音樂播放器,這時(shí)Activity先后執(zhí)行了onPause()->onStop(),應(yīng)用程序并沒有被銷毀,Logcat窗口如下所示:


而當(dāng)我們?cè)俅螁?dòng)應(yīng)用時(shí),則分別先后執(zhí)行了onRestart()->onStart()->onResume(),Logcat窗口如下所示:


這里我們引出一個(gè)問題,當(dāng)我們按Home鍵,再次進(jìn)入應(yīng)用時(shí),我們的應(yīng)用狀態(tài)應(yīng)該是和按Home鍵之前的狀態(tài)是一樣的,同樣為了方便理解,將ActivityDemo的代碼進(jìn)行修改,增加一個(gè)EditText。

修改main.xml布局文件,代碼如下:


android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:id="@+id/editText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

其他不變,運(yùn)行程序,在EditText輸入“Frankie”,如下圖:


這時(shí)按下Home鍵,再次啟動(dòng)應(yīng)用,EditText并沒有我們輸入的“Frankie”,如下圖:


顯然這不能稱得上是一個(gè)合格的應(yīng)用,我們需要在Activity方法里自己實(shí)現(xiàn),代碼如下:

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;

//定義一個(gè)String?類型用來存取我們EditText輸入的值

private String?mString;

public void onCreate(Bundle?savedInstanceState)?{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mEditText?=?(EditText)findViewById(R.id.editText);

Log.e(TAG,"start?onCreate~~~");

}

@Override

protect edvoidonStart()?{

super.onStart();

Log.e(TAG,"start?onStart~~~");

}

//當(dāng)按HOME鍵時(shí),然后再次啟動(dòng)應(yīng)用時(shí),我們要恢復(fù)先前狀態(tài)

@Override

protect edvoidonRestart()?{

super.onRestart();

mEditText.setText(mString);

Log.e(TAG,"start?onRestart~~~");

}

@Override

protecte dvoidonResume()?{

super.onResume();

Log.e(TAG,"start?onResume~~~");

}

//當(dāng)我們按HOME鍵時(shí),我在onPause方法里,將輸入的值賦給mString

@Override

protect edvoidonPause()?{

super.onPause();

mString?=?mEditText.getText().toString();

Log.e(TAG,"start?onPause~~~");

}

@Override

protect edvoidonStop()?{

super.onStop();

Log.e(TAG,"start?onStop~~~");

}

@Override

protect edvoidonDestroy()?{

super.onDestroy();

Log.e(TAG,"start?onDestroy~~~");

}

}

重新運(yùn)行應(yīng)用,按之前的操作,當(dāng)我們按Home鍵時(shí),再次啟動(dòng)應(yīng)用,EditText里有“Frankie”,如下圖:


OK,這時(shí)可以回頭看一下之前Activity的生命周期圖,相信大家已經(jīng)能夠基本掌握了。謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容