- 什么是活動(Activity)
- 活動(Activity)用法
- 活動(Activity)生命周期
- 活動(Activity)啟動模式
- 活動(Activity)管理
1,什么是活動(Activity)
活動(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.”大概意思是說,Activity是一個可以讓屏幕提供用戶交互動作的組件,比如打電話、照相、發送郵件和查看地圖等。簡單來說,我們在應用程序中能看到的內容,絕大多數都是Activity組件提供的。
2,活動(Activity)用法
3,活動(Activity)生命周期
- 運行狀態
- 暫停狀態
- 停止狀態
- 銷毀狀態
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onDestroy()
- onRestart()
- 完整生存期:活動在onCreate()和onDestroy()方法之間所經歷的
- 可見生存期:活動在onStart()和onStop()方法之間所經歷的
- 交互活動期:活動在onResume()和onPause()方法之間所經歷的
如圖:

讓我們用代碼來體驗下Activity的生命周期,首先我們先創建一個類來繼承Activity父類,用日志來打印出活動的七個回調方法。定義了兩個按鈕,一個是啟動正常的Activity,一個啟動對話框的Activity,布局界面代碼就不給出了,然后編寫點擊事件代碼。
1 public class MainActivity extends Activity { 2 3 public static final String TAG = "MainActivity"; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 Log.d(TAG, "onCreate"); 9 10 requestWindowFeature(Window.FEATURE_NO_TITLE); 11 setContentView(R.layout.activity_main); 12 if (savedInstanceState != null) { 13 String tempData = savedInstanceState.getString("data_key"); 14 Log.d(TAG, tempData); 15 } 16 Button startNormalActivity = (Button) findViewById(R.id.start_normal_activity); 17 Button startDialogActivity = (Button) findViewById(R.id.start_dialog_activity); 18 startNormalActivity.setOnClickListener(new OnClickListener() { 19 @Override 20 public void onClick(View v) { 21 Intent intent = new Intent(MainActivity.this, NormalActivity.class); 22 startActivity(intent); 23 } 24 }); 25 startDialogActivity.setOnClickListener(new OnClickListener() { 26 @Override 27 public void onClick(View v) { 28 Intent intent = new Intent(MainActivity.this, DialogActivity.class); 29 startActivity(intent); 30 } 31 }); 32 33 } 34 35 @Override 36 protected void onStart() { 37 super.onStart(); 38 Log.d(TAG, "onStart"); 39 } 40 41 @Override 42 protected void onResume() { 43 super.onResume(); 44 Log.d(TAG, "onResume"); 45 } 46 47 @Override 48 protected void onPause() { 49 super.onPause(); 50 Log.d(TAG, "onPause"); 51 } 52 53 @Override 54 protected void onStop() { 55 super.onStop(); 56 Log.d(TAG, "onStop"); 57 } 58 59 @Override 60 protected void onDestroy() { 61 super.onDestroy(); 62 Log.d(TAG, "onDestroy"); 63 } 64 65 @Override 66 protected void onRestart() { 67 super.onRestart(); 68 Log.d(TAG, "onRestart"); 69 } 70 71 @Override 72 protected void onSaveInstanceState(Bundle outState) { 73 super.onSaveInstanceState(outState); 74 String tempData = "Something you just typed"; 75 outState.putString("data_key", tempData); 76 } 77 }
1 @Override 2 protected void onSaveInstanceState(Bundle outState) { 3 super.onSaveInstanceState(outState); 4 String tempData = "Something you just typed"; 5 outState.putString("data_key", tempData); 6 }
執行以上代碼,數據是保存下來了,那么應該在哪里恢復呢,你可以細心的觀察到,在onCreate()方法里其實也有一個Bundle類型的參數,這個參數在一般情況下都是null,但是當活動被系統回收之前有通過onSaveInstanceState()方法來保存數據的話,這個參數就會帶有之前所保存的全部數據,我們只需要再通過相應的取值方法來將數據取出即可。
1 if (savedInstanceState != null) { 2 String tempData = savedInstanceState.getString("data_key"); 3 Log.d(TAG, tempData); 4 }
4,活動(Activity)啟動模式
5,活動(Activity)管理
1 public class BaseActivity extends Activity{ 2 3 private boolean allowFullScreen=true; //是否允許全屏 4 private boolean allowDestroy=true; //是否允許銷毀 5 6 @SuppressWarnings("unused") 7 private View view; 8 9 public boolean isAllowFullScreen() { //獲取是否全屏 10 return allowFullScreen; 11 } 12 13 public void setAllowFullScreen(boolean allowFullScreen) { //設置是否全屏 14 this.allowFullScreen = allowFullScreen; 15 } 16 17 public boolean isAllowDestroy() { //獲取是否銷毀 18 return allowDestroy; 19 } 20 21 public void setAllowDestroy(boolean allowDestroy) { //設置是否銷毀 22 this.allowDestroy = allowDestroy; 23 } 24 25 public void setAllowDestroy(boolean allowDestroy, View view) { //設置是否銷毀(重載) 26 this.allowDestroy = allowDestroy; 27 this.view = view; 28 } 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 allowFullScreen = true; 34 AppManager.getAppManager().addActivity(this); //添加Activity到堆棧 35 } 36 37 @Override 38 protected void onStop() { 39 super.onStop(); 40 } 41 42 @Override 43 protected void onStart() { 44 super.onStart(); 45 } 46 47 @Override 48 protected void onRestart() { 49 super.onRestart(); 50 } 51 52 @Override 53 protected void onResume() { 54 super.onResume(); 55 } 56 57 @Override 58 protected void onPause() { 59 super.onPause(); 60 } 61 62 @Override 63 protected void onDestroy() { 64 AppManager.getAppManager().finishActivity(this); // 結束Activity&從堆棧中移除 65 super.onDestroy(); 66 } 67 68 @Override 69 public boolean onKeyDown(int keyCode, KeyEvent event) { 70 if (keyCode == KeyEvent.KEYCODE_BACK) { //監控返回鍵,並且包含視圖組件 71 if (!allowDestroy) { 72 return false; 73 } 74 } 75 return super.onKeyDown(keyCode, event); 76 } 77 }
