標籤:android layout 介面 activity 生命週期
1、Activity簡介:
Activity可以簡單理解為android手機應用程式中的每一個介面,
其有相應的實現Activity類的java類檔案(相當於手機介面控制相關邏輯的檔案,類似flex頁面的指令檔,或者常見頁面形式中的js),
也有響應的布局xml檔案,預先設定好響應的布局控制項及其大小、顏色等屬性,
每一個Activity都需要在AndroidManifest.xml檔案中註冊,類似於javaweb中servlet,listener需要在web.xml中註冊一樣。
2、Activity的生命週期圖,如下:
簡單幾個階段說明:
1)、比方說開啟一個登入Activity,名為LoginActivity,該LoginActivity會先執行onCreate,onStart,onResume方法,完成登入介面的初始化。
2)、若使用者點擊返回導航到主菜單,則執行LoginActivity的onPause,OnStop(使用者看不到登入介面時執行),OnDestory方法
onRestart在下面使用案例說明。
3、案例說明:
簡介:總共兩個介面,一個主介面,名為TestLifeCycleActivity(需在),一個點擊主介面上的按鈕轉到的子介面,名為TestLifeCycleActivity2,代碼及布局檔案如下:
TestLifeCycleActivity.java
package com.example.helloworld;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class TestLifeCycleActivity extends Activity {private int i = 1;private Button test_life_cycle_btn1 = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test_life_cycle);System.err.println(i + "、" + "TestLifeCycleActivity onCreate");i++;findView();test_life_cycle_btn1.setOnClickListener(new OnClickListener() {Intent intent = new Intent(TestLifeCycleActivity.this,TestLifeCycleActivity2.class);@Overridepublic void onClick(View view) {TestLifeCycleActivity.this.startActivity(intent);}});}private void findView(){test_life_cycle_btn1 = (Button) findViewById(R.id.test_life_cycle_btn1);}@Overrideprotected void onDestroy() {super.onDestroy();System.err.println(i + "、" + "TestLifeCycleActivity onDestroy");i++;}@Overrideprotected void onPause() {super.onPause();System.err.println(i + "、" + "TestLifeCycleActivity onPause");i++;}@Overrideprotected void onRestart() {super.onRestart();System.err.println(i + "、" + "TestLifeCycleActivity onRestart");i++;}@Overrideprotected void onResume() {super.onResume();System.err.println(i + "、" + "TestLifeCycleActivity onResume");i++;}@Overrideprotected void onStart() {super.onStart();System.err.println(i + "、" + "TestLifeCycleActivity onStart");i++;}@Overrideprotected void onStop() {super.onStop();System.err.println(i + "、" + "TestLifeCycleActivity onStop");i++;}}
TestLifeCycleActivity2 .java
package com.example.helloworld;import android.app.Activity;import android.os.Bundle;public class TestLifeCycleActivity2 extends Activity {private int i = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test_life_cycle2);System.err.println(i + "、" + "TestLifeCycleActivity2 onCreate");i++;}@Overrideprotected void onDestroy() {super.onDestroy();System.err.println(i + "、" + "TestLifeCycleActivity2 onDestroy");i++;}@Overrideprotected void onPause() {super.onPause();System.err.println(i + "、" + "TestLifeCycleActivity2 onPause");i++;}@Overrideprotected void onRestart() {super.onRestart();System.err.println(i + "、" + "TestLifeCycleActivity2 onRestart");i++;}@Overrideprotected void onResume() {super.onResume();System.err.println(i + "、" + "TestLifeCycleActivity2 onResume");i++;}@Overrideprotected void onStart() {super.onStart();System.err.println(i + "、" + "TestLifeCycleActivity2 onStart");i++;}@Overrideprotected void onStop() {super.onStop();System.err.println(i + "、" + "TestLifeCycleActivity2 onStop");i++;}}
TestLifeCycleActivity布局檔案,僅有一個按鈕,轉到子介面:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/test_life_cycle_btn1" android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="測試Activity生命週期"/></LinearLayout>
TestLifeCycleActivity2布局檔案如下,無內容:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
在AndroidManifest.xml中註冊主介面與子介面:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.helloworld.TestLifeCycleActivity" 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="com.example.helloworld.TestLifeCycleActivity2"> </activity> </application></manifest>
測試過程1——點擊主介面的按鈕,轉到子介面,然後點擊返回導航,回到主介面,各個生命週期的方法執行如下:
05-14 10:54:19.012: W/System.err(1862): 1、TestLifeCycleActivity onCreate05-14 10:54:19.012: W/System.err(1862): 2、TestLifeCycleActivity onStart05-14 10:54:19.041: W/System.err(1862): 3、TestLifeCycleActivity onResume -- 以上三個為主介面的初始化05-14 10:54:23.481: W/System.err(1862): 4、TestLifeCycleActivity onPause -- 點擊按鈕後將要轉到子介面,先暫停主介面05-14 10:54:25.241: W/System.err(1862): 1、TestLifeCycleActivity2 onCreate 05-14 10:54:25.251: W/System.err(1862): 2、TestLifeCycleActivity2 onStart05-14 10:54:25.281: W/System.err(1862): 3、TestLifeCycleActivity2 onResume -- 初始化子介面,此時子介面被渲染,佔據螢幕最前端05-14 10:54:27.551: W/System.err(1862): 5、TestLifeCycleActivity onStop -- 主介面不可見,執行主介面的stop方法05-14 10:55:11.742: W/System.err(1862): 4、TestLifeCycleActivity2 onPause -- 點擊返回導航時,暫停子介面05-14 10:55:11.802: W/System.err(1862): 6、TestLifeCycleActivity onRestart -- 執行主介面的restart方法05-14 10:55:11.802: W/System.err(1862): 7、TestLifeCycleActivity onStart -- 執行主介面的start方法05-14 10:55:11.813: W/System.err(1862): 8、TestLifeCycleActivity onResume -- 執行主介面的onResume方法,重新開始渲染主介面,此時主介面在螢幕最前端05-14 10:55:13.354: W/System.err(1862): 5、TestLifeCycleActivity2 onStop -- 子介面不可見,執行子介面onStop方法05-14 10:55:13.362: W/System.err(1862): 6、TestLifeCycleActivity2 onDestroy -- 銷毀子介面
測試過程2——點擊主介面的按鈕,將要彈出子介面,此時狂點返回導航,直至退出HelloWorld程式,返回到手機,主介面與子介面各個生命週期的方法執行如下:
05-14 10:56:50.281: W/System.err(1862): 1、TestLifeCycleActivity onCreate05-14 10:56:50.301: W/System.err(1862): 2、TestLifeCycleActivity onStart05-14 10:56:50.391: W/System.err(1862): 3、TestLifeCycleActivity onResume -- 主介面初始化渲染05-14 10:56:55.331: W/System.err(1862): 4、TestLifeCycleActivity onPause -- 子介面將要佔據手機螢幕,暫停主介面05-14 10:56:56.411: W/System.err(1862): 1、TestLifeCycleActivity2 onCreate 05-14 10:56:56.421: W/System.err(1862): 2、TestLifeCycleActivity2 onStart05-14 10:56:56.441: W/System.err(1862): 3、TestLifeCycleActivity2 onResume -- 子介面渲染05-14 10:56:58.051: W/System.err(1862): 4、TestLifeCycleActivity2 onPause -- 主介面將要佔據手機螢幕,暫停子介面05-14 10:56:58.261: W/System.err(1862): 5、TestLifeCycleActivity onResume -- 使用者點擊返回導航到主介面,執行主介面OnResume,因為子介面沒有來得及佔據手機螢幕,所以主介面此時仍然可見,則不會執行其OnStop方法05-14 10:56:59.111: W/System.err(1862): 5、TestLifeCycleActivity2 onStop -- 子介面不可見,執行onStop方法05-14 10:56:59.111: W/System.err(1862): 6、TestLifeCycleActivity2 onDestroy -- 銷毀子介面05-14 10:56:59.521: W/System.err(1862): 6、TestLifeCycleActivity onPause -- 使用者點擊多次返回導航,退回到主介面後,接著退出主介面,到手機案頭,則需暫停主介面05-14 10:57:04.571: W/System.err(1862): 7、TestLifeCycleActivity onStop -- 接著主介面不可見,停止主介面05-14 10:57:04.571: W/System.err(1862): 8、TestLifeCycleActivity onDestroy -- 接著銷毀主介面