關於activity的生命週期1

Diy_os發表於2015-10-22
anctivity的生命週期比較複雜,但是很好的理解其生命週期,對於我們設計效能優良的應用程式有很大的幫助。
下面是官方文件上的一幅比較經典的activity生命週期示意圖:


其中涉及到七個生命週期函式,下面摘自官方文件的介紹:
Method Description Killable? Next
onCreate() 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().

No onStart()
     onRestart() Called after your activity has been stopped, prior to it being started again.

Always followed by onStart()

No onStart()
onStart() Called when the activity is becoming visible to the user.

Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

No onResume()or onStop()
     onResume() 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().

No onPause()
onPause() 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.

Yes onResume()or
onStop()
onStop() 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, oronDestroy() if this activity is going away.

Yes onRestart()or
onDestroy()
onDestroy() 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. Yes nothing

文件上已經很瞭然介紹了各函式的作用,什麼時候被呼叫等,下面簡單的總結一下:
1.onCreate()
Activity首次被建立時被呼叫,用於設定activity的佈局檔案,繫結按鈕監聽器等一些靜態操作
2.onStart()
Activity對使用者可見時被呼叫
3.onResume()
Activity獲得使用者焦點,即使用者可以操作activity時被呼叫
4.onPause()
應用程式啟動了其他activity時被呼叫。一般用於當前activity中的資料
5.onStop()
activity不可見時被呼叫
6.onRestart()
已停止的activity重新啟動被呼叫
7.onDestroy()
呼叫activity的finish()方法或者android系統資源不足的時候被呼叫

我們透過簡單的實驗來說明一下:

下面的實驗是建立兩個activity,跳轉的過程中,透過activity生命週期函式來說明activity的生命過程。由於佈局很簡單,下面只給出截圖:
activity_main.xml:


two.xml:


MainActivity:(下面透過重寫activity生命週期函式,來列印各個過程)

點選(此處)摺疊或開啟

  1. package com.example.android02;

  2. import org.apache.commons.logging.Log;

  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.TextView;

  10. public class MainActivity extends Activity {

  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         //Log.i(TAG,"onCreat");
  16.         TextView text = (TextView)findViewById(R.id.text1);
  17.         text.setText(R.string.number1);
  18.         Button button = (Button)findViewById(R.id.one);
  19.         button.setText(R.string.change);
  20.         button.setOnClickListener(new wang());
  21.         
  22.     }
  23.     
  24.     class wang implements OnClickListener{
  25.         
  26.         public void onClick(View v){

  27.             Intent intent = new Intent();
  28.             intent.setClass(MainActivity.this,TwoActivity.class);
  29.             MainActivity.this.startActivity(intent);

  30.             }
  31.         
  32.         //public
  33.         

  34.     }
  35.     
  36.     protected void onStart() {
  37.         super.onStart();
  38.          System.out.println("first activity --》 onstart");
  39.         // The activity is about to become visible.
  40.     }
  41.     @Override
  42.     protected void onResume() {
  43.         super.onResume();
  44.         System.out.println("first activity -->onresume");
  45.         // The activity has become visible (it is now "resumed").
  46.     }
  47.     @Override
  48.     protected void onPause() {
  49.         super.onPause();
  50.         System.out.println("first activity -->onpause");
  51.         // Another activity is taking focus (this activity is about to be "paused").
  52.     }
  53.     @Override
  54.     protected void onStop() {
  55.         super.onStop();
  56.         System.out.println("first activity -->onstop");
  57.         // The activity is no longer visible (it is now "stopped")
  58.     }
  59.      protected void onRestart(){
        super.onRestart();
        System.out.println("first activity -->restart");
       
        }
  60.     @Override
  61.     protected void onDestroy() {
  62.         super.onDestroy();
  63.         System.out.println("first activity -->ondestory");
  64.         // The activity is about to be destroyed.
  65.     }
  66. }

TwoActivity:

點選(此處)摺疊或開啟

  1. package com.example.android02;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.TextView;

  6. public class TwoActivity extends Activity{
  7.     
  8.     
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.two);
  13.         TextView text = (TextView)findViewById(R.id.text2);
  14.         text.setText(R.string.number2);
  15.         @SuppressWarnings("unused")
  16.         Button button = (Button)findViewById(R.string.button2);
  17.     }
  18.     
  19.     protected void onStart() {
  20.         super.onStart();
  21.          System.out.println("second activity --》 onstart");
  22.         // The activity is about to become visible.
  23.     }
  24.     @Override
  25.     protected void onResume() {
  26.         super.onResume();
  27.         System.out.println("second activity -->onresume");
  28.         // The activity has become visible (it is now "resumed").
  29.     }
  30.     @Override
  31.     protected void onPause() {
  32.         super.onPause();
  33.         System.out.println("second activity -->onpause");
  34.         // Another activity is taking focus (this activity is about to be "paused").
  35.     }
  36.     @Override
  37.     protected void onStop() {
  38.         super.onStop();
  39.         System.out.println("second activity -->onstop");
  40.         // The activity is no longer visible (it is now "stopped")
  41.     }
  42.      protected void onRestart(){
        super.onRestart();
        System.out.println("second activity -->restart");
       
        }
  43.     @Override
  44.     protected void onDestroy() {
  45.         super.onDestroy();
  46.         System.out.println("second activity -->ondestory");
  47.         // The activity is about to be destroyed.
  48.     }
  49. }



此時我們透過執行程式,透過Logcat可以看出列印出的各個生命週期階段:

啟動後,日誌輸出:(第一個activity經歷了oncreate,onstart,onresume,獲得焦點)

點選按鈕然後進入第二個activity:(第一個activity可以是可見,但是失去了焦點,第二個activity獲得了焦點)

此時點選back鍵:(此時第二個activity會失去焦點。而第一個activity之前被stop,此時會onrestart,重新獲得焦點。從上面的activity生命圖解可以很清楚的看出整個過程。這方面涉及到activity和Task相關知識,task以棧的形式存在,先啟動的activity會先入棧,最後啟動的activity會放在棧頂。上面第二個activity進棧的時候,此時完全遮蓋了第一個activity,所以導致了第一個activity被onstop,但是沒有被銷燬,這也是當第二個activity出棧,失去焦點後,第一個activity restart,而沒有oncreate的原因)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29876893/viewspace-1815730/,如需轉載,請註明出處,否則將追究法律責任。

相關文章