實作說明: 試著觀察生命週期。我們運行兩個activity,並複寫所有生命週期中的回調方法,通過activity A啟動activity B和activity B啟動activity A觀察兩個activity的生命週期變化。
Activity A(HelloActivityActivity):
- package test.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class HelloActivityActivity extends Activity {
- /** Called when the activity is first created. */
- private Button mBtn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- //初始化所有成員變數
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- System.out.println("Activity1---onCreate");
- mBtn = (Button)findViewById(R.id.button1);
- }
- public void GoToActivity2(View view){
- Intent intent = new Intent(HelloActivityActivity.this , activity2.class);
- startActivity(intent);
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- //釋放所有資源
- super.onDestroy();
- System.out.println("Activity1---onDestory");
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- //此方法執行後,activity失去用戶焦點,
- super.onPause();
- System.out.println("Activity1---onPause");
- }
- @Override
- protected void onRestart() {
- // TODO Auto-generated method stub
- super.onRestart();
- System.out.println("Activity1---onRestart");
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- //此方法執行後,activity取得用戶焦點,
- super.onResume();
- System.out.println("Activity1---onResume");
- }
- @Override
- protected void onStart() {
- // TODO Auto-generated method stub
- //此方法執行後,用戶看的見activity,置方法監控影響UI的事件,隨時隨環境變化UI
- super.onStart();
- System.out.println("Activity1---onStart");
- }
- @Override
- protected void onStop() {
- // TODO Auto-generated method stub
- //此方法執行後,用戶無法看見activity,釋放所有在onStart()所有監控影響UI的方法
- super.onStop();
- System.out.println("Activity1---onStop");
- }
- }
Activity B(activity2):
- package test.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class activity2 extends Activity {
- private Button mBtn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout2);
- System.out.println("activity2---onCreate");
- mBtn = (Button)findViewById(R.id.button1);
- }
- public void BackToActivity1(View view){
- Intent intent = new Intent(activity2.this , HelloActivityActivity.class);
- startActivity(intent);
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- System.out.println("activity2---onDestory");
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- System.out.println("activtiy2---onPause");
- }
- @Override
- protected void onRestart() {
- // TODO Auto-generated method stub
- super.onRestart();
- System.out.println("activity2---onRestart");
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- System.out.println("activity2---onResume");
- }
- @Override
- protected void onStart() {
- // TODO Auto-generated method stub
- super.onStart();
- System.out.println("activity2---onStart");
- }
- @Override
- protected void onStop() {
- // TODO Auto-generated method stub
- super.onStop();
- System.out.println("activity2---onStop");
- }
- }
實驗一:執行activity A後關閉,觀察生命週期變化。
變化如下圖。
發現一個activity開始運行到出現在屏幕上會經過生命週期為onCreate()->onStart()->onResume(),關閉activity會經過生命週期為onPause()->onStop()->onDestory()。實驗二:執行activity A後,A啟動activity B,B再啟動A,接著按"BACK"直到結束整個activity,觀察生命週期變化。
變化如下圖。
觀察activity A啟動時會經過生命週期onCreate()->onStart()->onResume(),A啟動activity B會經過生命週期就比較複雜,首先要啟動B,A會先onPause(),接著換B要運行了,B會執行onCreate()->onStart()->onResume(),B取得用戶焦點,因此,A執行onStop()。執行到此我們來想想task狀態,task目前總共放有兩個activity,下層是最先執行的A,頂層是從A啟動的B,而從B啟動的A正在執行。現在接著按"BACK"按鈕,觀察A先執行onPause()讓在task頂層的B恢復,B執行onRestart()->onStart()->onResume(),A執行onStop()->onDedtory(),A結束,再按一次"BACK"按鈕,觀察B一樣先執行onPause()讓task中剩下的A恢復,A執行onRestart()->onStart()->onResume(),B執行onStop()->onDestory(),B結束,再按一次"BACK"按鈕,應用程式關閉,A執行onPause()->onStop()->onDestory(),A結束。
沒有留言:
張貼留言