国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android編程中activity的完整生命周期實(shí)例詳解

2020-04-11 11:09:40
字體:
供稿:網(wǎng)友

本文實(shí)例分析了Android編程中activity的完整生命周期。分享給大家供大家參考,具體如下:

android中 activity有自己的生命周期,對這些知識的學(xué)習(xí)可以幫助我們在今后寫程序的時候,更好的理解其中遇到的一些錯誤。這篇文章很長,希望不要耽誤大家的時間~

今天不會涉及太多關(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. ”也就是用戶用來交互的每一個窗口,用戶當(dāng)前正在進(jìn)行的一個操作。

2 back-stack  用戶通過觸摸程序會通過application launcher來啟動一個activity A,啟動的activity A會被壓入棧頂,如果當(dāng)前的activity A再啟動一個新的activity B,那么此時A調(diào)用onStop函數(shù),系統(tǒng)會維護(hù)這個activity信息.當(dāng)用戶按下back鍵的時候,back stack會進(jìn)行一個pop的操作,并且調(diào)用A的onResume()  具體的進(jìn)出棧細(xì)節(jié),以后會詳細(xì)介紹。

3 Tasks  當(dāng)用戶處于某個activi提: Activity A在名稱為"TaskOne應(yīng)用ty的時候,按下HOME鍵用戶返回到launcher,此時,如果用戶再觸摸新的應(yīng)用,則新建一個Task,一個back stack就代表一個task.不同程序的activity可以壓入同一個棧中,也就是說可以組成一個task,比如你的程序啟動了一個系統(tǒng)自帶的發(fā)短信的activity,給用戶的感覺就是發(fā)短信好像是你的程序中的一個功能一樣。

注釋:以上的行為均為系統(tǒng)的默認(rèn)設(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)對應(yīng)的操作,下面介紹各個方法的調(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().
//當(dāng)activity第一次被創(chuàng)建的時候調(diào)用。你應(yīng)該在這個方法里進(jìn)行所有的靜態(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().
//當(dāng)activity將被啟動與用戶交互的時候被調(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)即將重新開始以前的activity的時候被調(diào)用(不懂,自己的理解是:當(dāng)當(dāng)前activity要啟動新的activity的時候被調(diào)用),典型的應(yīng)用是用來將還未保存的數(shù)據(jù)提交到當(dāng)前的數(shù)據(jù),(意思就是保存數(shù)據(jù)更新),停止animations和其他可能耗費(fèi)CPU的操作。對此方法的實(shí)現(xiàn)必須快速因?yàn)橄聜€activity直到此方法返回才會被重新開始。

當(dāng)activity從back(翻譯后臺不合適)轉(zhuǎn)到front(與用戶交互的狀態(tài))的時候,onResume方法會在onPause方法之后被調(diào)用

當(dāng)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.

//當(dāng)activity對用戶不再可見時被調(diào)用,因?yàn)榱硪粋€activity已經(jīng)重新開始并且覆蓋了當(dāng)前activity(在棧中)。當(dāng)有新的activity被啟動,或者一個存在的activity重新回到前臺狀態(tài),又或者當(dāng)前的activity將被銷毀。如果activity要返回前臺和用戶進(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被消亡時接收到的最后一個方法。調(diào)用此方法有兩種情形:一是 activity將要完成,可通過調(diào)用finish方法實(shí)現(xiàn)。而是系統(tǒng)銷毀activity的實(shí)例來釋放空間。可以使用isFinish方法來區(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會被強(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是否要銷毀     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

然后單擊按鈕進(jìn)入Activity01

(可見activity先暫停并且不會被釋放,實(shí)際是個新activity壓棧過程,然后新的activity開始,應(yīng)該是onCreate然后onStart,我打印語句寫錯了,細(xì)心朋友應(yīng)該看到了,當(dāng)舊的activity不可見時,調(diào)用其onStop方法)

再按返回鍵回到ActivityDemoActivity

(返回后,處于棧頂?shù)腶ctivity01會執(zhí)行彈棧操作,顯示將會被destroy)

再按返回鍵 回到桌面

其實(shí)并不復(fù)雜的東邪寫的有些長了,但是基本上的顯示了activity的完整的生命周期。

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南汇区| 玛纳斯县| 安宁市| 泌阳县| 蚌埠市| 榆树市| 镇原县| 岐山县| 光泽县| 保亭| 吐鲁番市| 阜康市| 梁山县| 清涧县| 大兴区| 泉州市| 灌云县| 阜南县| 松桃| 乐山市| 大足县| 绵竹市| 阜宁县| 沙河市| 墨竹工卡县| 苍山县| 敦化市| 额尔古纳市| 庆城县| 西乌珠穆沁旗| 泉州市| 峨眉山市| 辰溪县| 望奎县| 广东省| 青岛市| 新干县| 达拉特旗| 兰溪市| 余姚市| 克东县|