Android編程中activity的完整生命周期實例詳解
本文實例分析了Android編程中activity的完整生命周期。分享給大家供大家參考,具體如下:
android中 activity有自己的生命周期,對這些知識的學習可以幫助我們在今后寫程序的時候,更好的理解其中遇到的一些錯誤。這篇文章很長,希望不要耽誤大家的時間~
今天不會涉及太多關(guān)于activity棧的東西,主要說activity自身的生命周期
區(qū)分幾個概念
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. ”也就是用戶用來交互的每一個窗口,用戶當前正在進行的一個操作。
2 back-stack 用戶通過觸摸程序會通過application launcher來啟動一個activity A,啟動的activity A會被壓入棧頂,如果當前的activity A再啟動一個新的activity B,那么此時A調(diào)用onStop函數(shù),系統(tǒng)會維護這個activity信息.當用戶按下back鍵的時候,back stack會進行一個pop的操作,并且調(diào)用A的onResume() 具體的進出棧細節(jié),以后會詳細介紹。
3 Tasks 當用戶處于某個activi提: Activity A在名稱為"TaskOne應用ty的時候,按下HOME鍵用戶返回到launcher,此時,如果用戶再觸摸新的應用,則新建一個Task,一個back stack就代表一個task.不同程序的activity可以壓入同一個棧中,也就是說可以組成一個task,比如你的程序啟動了一個系統(tǒng)自帶的發(fā)短信的activity,給用戶的感覺就是發(fā)短信好像是你的程序中的一個功能一樣。
注釋:以上的行為均為系統(tǒng)的默認設(shè)置,有兩種方式可以改變activity的行為,加入A啟動B,一是在B的manifest設(shè)置中,改變行為,另一種是在Activity發(fā)起的intent中指定要啟動的activity設(shè)置,其中Intent的優(yōu)先級要高于manifest.xml文件,并且有些mode在并不是同時都存在于兩種方式中。
android的生命周期包括 onCreate onStart onRestart onResume onPause onStop onDestroy ,activity在聲明周期中會調(diào)用以上方法來執(zhí)行不同狀態(tài)對應的操作,下面介紹各個方法的調(diào)用時間
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().
//當activity第一次被創(chuàng)建的時候調(diào)用。你應該在這個方法里進行所有的靜態(tài)創(chuàng)建,創(chuàng)建views,(通過setContnetView方法)向lists綁定數(shù)據(jù)等等。如果存在保存的狀態(tài)的話,該方法也提供給你一個包含activity最近狀態(tài)的一個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)用,在重新開始之前調(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().
//當activity將被啟動與用戶交互的時候被調(diào)用。此刻你的activity處于activity棧的頂端,用于用戶輸入,永遠///在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.
//當系統(tǒng)即將重新開始以前的activity的時候被調(diào)用(不懂,自己的理解是:當當前activity要啟動新的activity的時候被調(diào)用),典型的應用是用來將還未保存的數(shù)據(jù)提交到當前的數(shù)據(jù),(意思就是保存數(shù)據(jù)更新),停止animations和其他可能耗費CPU的操作。對此方法的實現(xiàn)必須快速因為下個activity直到此方法返回才會被重新開始。
當activity從back(翻譯后臺不合適)轉(zhuǎn)到front(與用戶交互的狀態(tài))的時候,onResume方法會在onPause方法之后被調(diào)用
當activity變?yōu)椴豢梢姷臅r候,onStop方法會在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.
//當activity對用戶不再可見時被調(diào)用,因為另一個activity已經(jīng)重新開始并且覆蓋了當前activity(在棧中)。當有新的activity被啟動,或者一個存在的activity重新回到前臺狀態(tài),又或者當前的activity將被銷毀。如果activity要返回前臺和用戶進行交互則在此方法后調(diào)用onReatart方法,如果當前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.
這是當你的activity被消亡時接收到的最后一個方法。調(diào)用此方法有兩種情形:一是 activity將要完成,可通過調(diào)用finish方法實現(xiàn)。而是系統(tǒng)銷毀activity的實例來釋放空間??梢允褂胕sFinish方法來區(qū)別兩種情形。這個方法常用在onPause方法中,來判斷activity是暫停還是將終止。后面的demo將會演示這個功能。
下圖是官網(wǎng)的一個生命周期的演示
好了 看一下我寫的一個演示的例子吧,
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); //感興趣的朋友可以取消下面的代碼注釋,來測試finish方法的使用,會發(fā)現(xiàn)第一個activity會被強制終止 //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是否要銷毀 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啟動 * */ 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是否要銷毀 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é)果,
操作過程是:啟動ActivityDemoActivity
然后單擊按鈕進入Activity01
(可見activity先暫停并且不會被釋放,實際是個新activity壓棧過程,然后新的activity開始,應該是onCreate然后onStart,我打印語句寫錯了,細心朋友應該看到了,當舊的activity不可見時,調(diào)用其onStop方法)
再按返回鍵回到ActivityDemoActivity
(返回后,處于棧頂?shù)腶ctivity01會執(zhí)行彈棧操作,顯示將會被destroy)
再按返回鍵 回到桌面
其實并不復雜的東邪寫的有些長了,但是基本上的顯示了activity的完整的生命周期。
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android實戰(zhàn)打飛機游戲之子彈生成與碰撞以及爆炸效果(5)
這篇文章主要為大家詳細介紹了Android實戰(zhàn)打飛機游戲之子彈生成與碰撞以及爆炸效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07Android學習小結(jié)之獲取被啟動的Activity傳回的數(shù)據(jù)
這篇文章主要介紹了獲取被啟動的Activity傳回的數(shù)據(jù),非常不錯,介紹的非常詳細,需要的朋友可以參考下2016-08-08詳解Android中通過Intent類實現(xiàn)組件間調(diào)用的方法
Intent能夠?qū)崿F(xiàn)應用間的數(shù)據(jù)交互與通訊,將實現(xiàn)者和調(diào)用者解耦,接下來就來詳解Android中通過Intent類實現(xiàn)組件間調(diào)用的方法,需要的朋友可以參考下2016-05-05Android開發(fā)之利用Activity實現(xiàn)Dialog對話框
這篇文章主要給大家介紹了Android開發(fā)之如何利用Activity實現(xiàn)Dialog對話框效果,文中給出了詳細的示例代碼,相信對大家的理解及學習具有一定的參考借鑒價值,有需要的朋友們下面來一起看看吧。2016-12-12