【移動應(yīng)用開發(fā)技術(shù)】Android系列教程之Activity的生命周期_第1頁
【移動應(yīng)用開發(fā)技術(shù)】Android系列教程之Activity的生命周期_第2頁
【移動應(yīng)用開發(fā)技術(shù)】Android系列教程之Activity的生命周期_第3頁
【移動應(yīng)用開發(fā)技術(shù)】Android系列教程之Activity的生命周期_第4頁
【移動應(yīng)用開發(fā)技術(shù)】Android系列教程之Activity的生命周期_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】Android系列教程之Activity的生命周期

通過上一節(jié)“Android系列教程之Android項目的目錄結(jié)構(gòu)”我們已經(jīng)知道了什么是Activity,那么為什么我們創(chuàng)建一個Activity的導(dǎo)出類的時候為什么都要覆蓋Activity的onCreate方法呢,為什么會在onPause()方法中保存一些當(dāng)前Activity中的變化,要弄明白這些就要先了解Activity的生命周期,也就是一個Activity才開始到結(jié)束都要經(jīng)過那些狀態(tài),下面通過一個例子了解Activity的聲明周期.一:Activity的生命周期方法

Android提供了很多Activity的生命周期方法,比如我們常用的onCreate、onPause、onResume等。這里主要介紹粗粒度的周期方法,諸如onPostCreate、onPostResume等這些細(xì)粒度的周期方法可以參考Android的API文檔,在你需要更細(xì)層次的控制的時候可以使用這些細(xì)粒度的方法。粗粒度的周期方法有以下幾個:onCreate()、onStart()、onResume()、onPause()、onStop()、onDestroy(),從名字上就可以看出來這些方法在什么時候執(zhí)行。二:測試Activity的生命周期方法的執(zhí)行順序為了能更明白上這幾個周期放的執(zhí)行順序,我們新建一個HelloWorld項目,在Activity中覆蓋這幾個方法,打印出日志就可以看出來執(zhí)行順序了1、新建HelloWorld項目2、修改activity_main.xml代碼如下:<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="第一個Activity"

/>

<Button

android:id="@+id/second"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="打開第二個Activity"

/>

</LinearLayout>這里主要是為增加一個文本顯示和一個按鈕用于顯示信息和操作。3、新建布局文件second.xml,內(nèi)容如下:<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="第二個Activity"

/>

<Button

android:id="@+id/exit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="退出"

/>

</LinearLayout>這里主要是為增加一個文本顯示和一個退出按鈕用于退出當(dāng)前Activity。4、新建一個Activity,名字為SecondActivity,要在AndroidManifest.xml也聲明一下,否則報錯:SecondActivity類內(nèi)容如下:

import

android.app.Activity;

import

android.os.Bundle;

import

android.util.Log;

import

android.view.View;

import

android.view.View.OnClickListener;

import

android.widget.Button;

public

class

SecondActivity

extends

Activity{

private

final

static

String

TAG

=

"SecondActivity";

protected

void

onCreate(Bundle

savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.second);

//退出按鈕

Button

btnExit

=

(Button)

findViewById(R.id.exit);

//為退出按鈕設(shè)置單擊事件

btnExit.setOnClickListener(new

OnClickListener()

{

@Override

public

void

onClick(View

arg0)

{

//關(guān)閉當(dāng)前Activity,也就是退出

finish();

}

});

}

//覆蓋

android.app.Activity.onStart方法

protected

void

onStart(){

super.onStart();

Log.v(TAG,

"onStart");

}

//覆蓋

android.app.Activity.onResume

protected

void

onResume(){

super.onResume();

Log.v(TAG,

"onResume");

}

protected

void

onPause(){

super.onPause();

Log.v(TAG,

"onPause");

}

protected

void

onStop(){

super.onStop();

Log.v(TAG,

"onStop");

}

protected

void

onDestroy(){

super.onDestroy();

Log.v(TAG,"onDestroy");

}

}我在各個周期方法里都加了日志信息,便于跟蹤Activity的運行過程5、修改HelloWorld類,內(nèi)容如下:import

android.os.Bundle;

import

android.app.Activity;

import

android.content.Intent;

import

android.util.Log;

import

android.view.View;

import

android.view.View.OnClickListener;

import

android.widget.Button;

public

class

HelloWorld

extends

Activity

{

private

final

static

String

TAG

=

"HelloWorld";

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//打開第二個Activity的按鈕

Button

btnSecond

=

(Button)

findViewById(R.id.second);

btnSecond.setOnClickListener(new

OnClickListener()

{

@Override

public

void

onClick(View

arg0)

{

//跳轉(zhuǎn)到SecondActivity

startActivity(new

Intent(HelloWorld.this,SecondActivity.class));

finish();//關(guān)閉當(dāng)前Activity

}

});

}

/*

*

重寫覆蓋

android.app.Activity里的方法

*

*/

protected

void

onStart(){

super.onStart();

Log.v(TAG,

"onStart");

}

protected

void

onResume(){

super.onResume();

Log.v(TAG,

"onResume");

}

protected

void

onPause(){

super.onPause();

Log.v(TAG,

"onPause");

}

protected

void

onStop(){

super.onStop();

Log.v(TAG,

"onStop");

}

protected

void

onDestroy(){

super.onDestroy();

Log.v(TAG,"onDestroy");

}

}6、運行程序,分析結(jié)果,發(fā)現(xiàn)當(dāng)程序啟動的時候,日志信息為下圖:

由此可見當(dāng)打開一個Activity的時候,其周期方法執(zhí)行順序為:onCreate()->onStart()->onResume(),現(xiàn)在點擊“打開第二個Activity”按鈕,看日志的輸出如下圖:

當(dāng)應(yīng)用從Helloworld這個Activity啟動SecondActivity的時候,Android會先執(zhí)行HelloWorld的onPause方法,然后依次執(zhí)行SecondActivity的onCreate()->onStart()->onResume()方法當(dāng)SecondActivity呈現(xiàn)到屏幕上的時候再一次執(zhí)行Helloworld的onStop()->onDestroy(),把HelloWorld從Activity棧中移除銷毀。這里值得提的就是HelloWorld中finish方法,因為執(zhí)行了他所以HelloWorld才會從Activity棧中移除銷毀,這樣當(dāng)你按“返回”鍵返回的時候就回不到HelloWorld這個Activity的界面了,而是直接回到的Android的應(yīng)用程序列表

。三:分析結(jié)果根據(jù)上面例子可見一個Activity在啟動的時候會執(zhí)行onCreate()->onStart()->onResume(),在結(jié)束(或離開)的時候會執(zhí)行onPause()->onStop()->onDestroy(),這就是一個Activity的生命周期。因此我們要在onCreate方法里把Activity的需要的東西準(zhǔn)備好,也就是初始化;在onResume里對Activity里的東西做一些調(diào)整;在onPause做一些清理和保存工作(保存持久狀態(tài)),因為這是最后的機會,因為onPause完成之前Android不會結(jié)束托管Activity類的進(jìn)程,而之后進(jìn)程可能被結(jié)束??偨Y(jié)一下這幾個周期方法的作用:

onCreate():創(chuàng)建Activity調(diào)用,用于Activity的初始化,還有個Bundle類型的參數(shù),可以訪問以前存儲的狀態(tài)。onStart():Activity在屏幕上對用戶可見時調(diào)用onResume():Activity開始和用戶交互的時候調(diào)用,這時該Activity是在Activity棧的頂部。onPause():Activity被暫停時調(diào)用,也就是你要對你能看到的這個

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論