標籤:icon style nbsp 6.4 draw activity resume bundle min
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(需在),一個點擊主介面上的button轉到的子介面,名為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布局檔案。僅有一個button。轉到子介面:
<?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——點擊主介面的button,轉到子介面,然後點擊返回導航,回到主介面。各個生命週期的方法運行例如以下:
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 -- 點擊button後將要轉到子介面。先暫停主介面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——點擊主介面的button,將要彈出子介面。此時狂點返回導航,直至退出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 -- 接著銷毀主介面
android之Activity的生命週期