Android編程中activity的完整生命周期實(shí)例詳解
本文實(shí)例分析了Android編程中activity的完整生命周期。分享給大家供大家參考,具體如下:
android中 activity有自己的生命周期,對(duì)這些知識(shí)的學(xué)習(xí)可以幫助我們?cè)诮窈髮?xiě)程序的時(shí)候,更好的理解其中遇到的一些錯(cuò)誤。這篇文章很長(zhǎng),希望不要耽誤大家的時(shí)間~
今天不會(huì)涉及太多關(guān)于activity棧的東西,主要說(shuō)activity自身的生命周期
區(qū)分幾個(gè)概念
1 Activity 官方解釋為 “An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. ”也就是用戶用來(lái)交互的每一個(gè)窗口,用戶當(dāng)前正在進(jìn)行的一個(gè)操作。
2 back-stack 用戶通過(guò)觸摸程序會(huì)通過(guò)application launcher來(lái)啟動(dòng)一個(gè)activity A,啟動(dòng)的activity A會(huì)被壓入棧頂,如果當(dāng)前的activity A再啟動(dòng)一個(gè)新的activity B,那么此時(shí)A調(diào)用onStop函數(shù),系統(tǒng)會(huì)維護(hù)這個(gè)activity信息.當(dāng)用戶按下back鍵的時(shí)候,back stack會(huì)進(jìn)行一個(gè)pop的操作,并且調(diào)用A的onResume() 具體的進(jìn)出棧細(xì)節(jié),以后會(huì)詳細(xì)介紹。
3 Tasks 當(dāng)用戶處于某個(gè)activi提: Activity A在名稱為"TaskOne應(yīng)用ty的時(shí)候,按下HOME鍵用戶返回到launcher,此時(shí),如果用戶再觸摸新的應(yīng)用,則新建一個(gè)Task,一個(gè)back stack就代表一個(gè)task.不同程序的activity可以壓入同一個(gè)棧中,也就是說(shuō)可以組成一個(gè)task,比如你的程序啟動(dòng)了一個(gè)系統(tǒng)自帶的發(fā)短信的activity,給用戶的感覺(jué)就是發(fā)短信好像是你的程序中的一個(gè)功能一樣。
注釋?zhuān)?span style="color: #ff0000">以上的行為均為系統(tǒng)的默認(rèn)設(shè)置,有兩種方式可以改變activity的行為,加入A啟動(dòng)B,一是在B的manifest設(shè)置中,改變行為,另一種是在Activity發(fā)起的intent中指定要啟動(dòng)的activity設(shè)置,其中Intent的優(yōu)先級(jí)要高于manifest.xml文件,并且有些mode在并不是同時(shí)都存在于兩種方式中。
android的生命周期包括 onCreate onStart onRestart onResume onPause onStop onDestroy ,activity在聲明周期中會(huì)調(diào)用以上方法來(lái)執(zhí)行不同狀態(tài)對(duì)應(yīng)的操作,下面介紹各個(gè)方法的調(diào)用時(shí)間
onCreate() 次狀態(tài)下activity不可被終結(jié)
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
Always followed by onStart().
//當(dāng)activity第一次被創(chuàng)建的時(shí)候調(diào)用。你應(yīng)該在這個(gè)方法里進(jìn)行所有的靜態(tài)創(chuàng)建,創(chuàng)建views,(通過(guò)setContnetView方法)向lists綁定數(shù)據(jù)等等。如果存在保存的狀態(tài)的話,該方法也提供給你一個(gè)包含activity最近狀態(tài)的一個(gè)bundle。onStart方法總是在此方法之后調(diào)用
onRestart() 次狀態(tài)下activity不可被終結(jié)
Called after your activity has been stopped, prior to it being started again.
Always followed by onStart()
//在你的activity停止后被調(diào)用,在重新開(kāi)始之前調(diào)用
onResume() 次狀態(tài)下activity不可被終結(jié)
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
Always followed by onPause().
//當(dāng)activity將被啟動(dòng)與用戶交互的時(shí)候被調(diào)用。此刻你的activity處于activity棧的頂端,用于用戶輸入,永遠(yuǎn)///在onPause之后被調(diào)用
onPause() 次狀態(tài)下activity不可被終結(jié) ,android HoneyComb系統(tǒng)除外
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.
//當(dāng)系統(tǒng)即將重新開(kāi)始以前的activity的時(shí)候被調(diào)用(不懂,自己的理解是:當(dāng)當(dāng)前activity要啟動(dòng)新的activity的時(shí)候被調(diào)用),典型的應(yīng)用是用來(lái)將還未保存的數(shù)據(jù)提交到當(dāng)前的數(shù)據(jù),(意思就是保存數(shù)據(jù)更新),停止animations和其他可能耗費(fèi)CPU的操作。對(duì)此方法的實(shí)現(xiàn)必須快速因?yàn)橄聜€(gè)activity直到此方法返回才會(huì)被重新開(kāi)始。
當(dāng)activity從back(翻譯后臺(tái)不合適)轉(zhuǎn)到front(與用戶交互的狀態(tài))的時(shí)候,onResume方法會(huì)在onPause方法之后被調(diào)用
當(dāng)activity變?yōu)椴豢梢?jiàn)的時(shí)候,onStop方法會(huì)在onPause之后被調(diào)用
onStop() 次狀態(tài)下activity可以被終結(jié)
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.
//當(dāng)activity對(duì)用戶不再可見(jiàn)時(shí)被調(diào)用,因?yàn)榱硪粋€(gè)activity已經(jīng)重新開(kāi)始并且覆蓋了當(dāng)前activity(在棧中)。當(dāng)有新的activity被啟動(dòng),或者一個(gè)存在的activity重新回到前臺(tái)狀態(tài),又或者當(dāng)前的activity將被銷(xiāo)毀。如果activity要返回前臺(tái)和用戶進(jìn)行交互則在此方法后調(diào)用onReatart方法,如果當(dāng)前activity要消亡,則onDestroy方法將在此方法后被調(diào)用
onDestroy() 次狀態(tài)下activity可以被終結(jié)
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
這是當(dāng)你的activity被消亡時(shí)接收到的最后一個(gè)方法。調(diào)用此方法有兩種情形:一是 activity將要完成,可通過(guò)調(diào)用finish方法實(shí)現(xiàn)。而是系統(tǒng)銷(xiāo)毀activity的實(shí)例來(lái)釋放空間??梢允褂胕sFinish方法來(lái)區(qū)別兩種情形。這個(gè)方法常用在onPause方法中,來(lái)判斷activity是暫停還是將終止。后面的demo將會(huì)演示這個(gè)功能。
下圖是官網(wǎng)的一個(gè)生命周期的演示
好了 看一下我寫(xiě)的一個(gè)演示的例子吧,
MainFest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="uni.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ActivityDemoActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity01" android:label="@string/app_name"> </activity> </application> </manifest>
布局文件 main.xml
<?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" /> <Button android:id="@+id/Button_A" android:text="GO to activity 2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
activity01.xml
<?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" /> <Button android:id="@+id/Button_A" android:text="GO to activity 2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
String.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ActivityDemoActivity!</string> <string name="app_name">ActivityDemo</string> <string name="activity01">this is activity 01</string> </resources>
ActivityDemoActivity.java
/* * @author octobershiner * 2011 07 29 * SE.HIT * 演示完整的activity的聲明周期,以及isFinish方法的調(diào)用 * 此activity為程序入口activity * */ package uni.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ActivityDemoActivity extends Activity { /** Called when the activity is first created. */ private static final String TAG = "demo"; private Button button_A; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button_A = (Button)this.findViewById(R.id.Button_A); button_A.setOnClickListener(new myButtonListener()); } private class myButtonListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(ActivityDemoActivity.this, Activity01.class); ActivityDemoActivity.this.startActivity(intent); //感興趣的朋友可以取消下面的代碼注釋?zhuān)瑏?lái)測(cè)試finish方法的使用,會(huì)發(fā)現(xiàn)第一個(gè)activity會(huì)被強(qiáng)制終止 //ActivityDemoActivity.this.finish(); } }; protected void onStart(){ super.onStart(); Log.i(TAG, "The activityDemo state---->onStart"); } protected void onRestart(){ super.onRestart(); Log.i(TAG, "The activityDemo state---->onReatart"); } protected void onResume(){ super.onResume(); Log.i(TAG, "The activityDemo state---->onResume"); } protected void onPause(){ super.onPause(); //調(diào)用isFinishing方法,判斷activity是否要銷(xiāo)毀 Log.i(TAG, "The activityDemo state---->onPause"); if(isFinishing()){ Log.w(TAG, "The activityDemo will be destroyed!"); }else{ Log.w(TAG, "The activityDemo is just pausing!"); } } protected void onStop(){ super.onStop(); Log.i(TAG, "The activityDemo state---->onStop"); } protected void onDestroy(){ super.onDestroy(); Log.i(TAG, "The activityDemo state---->onDestroy"); } }
Activity01.java
/* * @author octobershiner * 2011 07 29 * SE.HIT * 演示完整的activity的聲明周期,以及isFinish方法的調(diào)用 * 此activity可由ActivityDemoActivity啟動(dòng) * */ package uni.activity; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class Activity01 extends Activity{ private static final String TAG = "demo"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); Log.d(TAG, "The activity01 state---->onStart"); } protected void onStart(){ super.onStart(); Log.d(TAG, "The activity01 state---->onStart"); } protected void onRestart(){ super.onRestart(); Log.d(TAG, "The activity01 state---->onReatart"); } protected void onResume(){ super.onResume(); Log.d(TAG, "The activity01 state---->onResume"); } protected void onPause(){ super.onPause(); Log.d(TAG, "The activity01 state---->onPause"); //調(diào)用isFinishing方法,判斷activity是否要銷(xiāo)毀 if(isFinishing()){ Log.w(TAG, "The activity01 will be destroyed!"); }else{ Log.w(TAG, "The activity01 is just pausing!"); } } protected void onStop(){ super.onStop(); Log.d(TAG, "The activity01 state---->onStop"); } protected void onDestroy(){ super.onDestroy(); Log.d(TAG, "The activity01 state---->onDestroy"); } }
下面是演示的結(jié)果,
操作過(guò)程是:?jiǎn)?dòng)ActivityDemoActivity
然后單擊按鈕進(jìn)入Activity01
(可見(jiàn)activity先暫停并且不會(huì)被釋放,實(shí)際是個(gè)新activity壓棧過(guò)程,然后新的activity開(kāi)始,應(yīng)該是onCreate然后onStart,我打印語(yǔ)句寫(xiě)錯(cuò)了,細(xì)心朋友應(yīng)該看到了,當(dāng)舊的activity不可見(jiàn)時(shí),調(diào)用其onStop方法)
再按返回鍵回到ActivityDemoActivity
(返回后,處于棧頂?shù)腶ctivity01會(huì)執(zhí)行彈棧操作,顯示將會(huì)被destroy)
再按返回鍵 回到桌面
其實(shí)并不復(fù)雜的東邪寫(xiě)的有些長(zhǎng)了,但是基本上的顯示了activity的完整的生命周期。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android實(shí)戰(zhàn)打飛機(jī)游戲之子彈生成與碰撞以及爆炸效果(5)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)打飛機(jī)游戲之子彈生成與碰撞以及爆炸效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android學(xué)習(xí)小結(jié)之獲取被啟動(dòng)的Activity傳回的數(shù)據(jù)
這篇文章主要介紹了獲取被啟動(dòng)的Activity傳回的數(shù)據(jù),非常不錯(cuò),介紹的非常詳細(xì),需要的朋友可以參考下2016-08-08詳解Android如何實(shí)現(xiàn)不同大小的圓角
在開(kāi)發(fā)過(guò)程中,設(shè)計(jì)常常會(huì)有一些比較炫酷的想法,比如兩邊不一樣大小的圓角啦,甚至四角的radius各不相同,對(duì)于這種情況我們?cè)撛趺磳?shí)現(xiàn)呢,本文小編就和大家來(lái)聊聊,需要的朋友可以參考下2023-08-08Android5.0 旋轉(zhuǎn)菜單實(shí)例詳解
這篇文章主要介紹了 Android5.0 旋轉(zhuǎn)菜單的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12詳解Android中通過(guò)Intent類(lèi)實(shí)現(xiàn)組件間調(diào)用的方法
Intent能夠?qū)崿F(xiàn)應(yīng)用間的數(shù)據(jù)交互與通訊,將實(shí)現(xiàn)者和調(diào)用者解耦,接下來(lái)就來(lái)詳解Android中通過(guò)Intent類(lèi)實(shí)現(xiàn)組件間調(diào)用的方法,需要的朋友可以參考下2016-05-05Android開(kāi)發(fā)之利用Activity實(shí)現(xiàn)Dialog對(duì)話框
這篇文章主要給大家介紹了Android開(kāi)發(fā)之如何利用Activity實(shí)現(xiàn)Dialog對(duì)話框效果,文中給出了詳細(xì)的示例代碼,相信對(duì)大家的理解及學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2016-12-12