利用Android的Log 演示一個activity的生命周期代碼 – Android移動開發技術文章_手機開發 Android移動開發教學課程

代碼: 
//DemoActivity.java  
 
package uni.activity; 
/*
  @author octobershiner
  2011 7 22
  SE.HIT
*/ 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
 
public class ActivityDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
     
    private static final String TAG = "demo"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Log.d("demo", "this is a test string "); 
    } 
 
    protected void onStart(){ 
        super.onStart(); 
        Log.i(TAG, "The activity state—->onStart"); 
    } 
     
    protected void onRestart(){ 
        super.onRestart(); 
        Log.i(TAG, "The activity state—->onReatart"); 
    } 
 
    protected void onResume(){ 
        super.onResume(); 
        Log.i(TAG, "The activity state—->onResume"); 
    } 
 
    protected void onPause(){ 
        super.onPause(); 
        Log.i(TAG, "The activity state—->onPause"); 
    } 
 
    protected void onStop(){ 
        super.onStop(); 
        Log.i(TAG, "The activity state—->onStop"); 
    } 
 
    protected void onDestroy(){ 
        super.onDestroy(); 
        Log.i(TAG, "The activity state—->onDestroy"); 
    } 
 
     

代碼:
//DemoActivity.java

package uni.activity;
/*
  @author octobershiner
  2011 7 22
  SE.HIT
*/
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class ActivityDemoActivity extends Activity {
    /** Called when the activity is first created. */
   
    private static final String TAG = "demo";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("demo", "this is a test string ");
    }

    protected void onStart(){
        super.onStart();
        Log.i(TAG, "The activity state—->onStart");
    }
   
    protected void onRestart(){
        super.onRestart();
        Log.i(TAG, "The activity state—->onReatart");
    }

    protected void onResume(){
        super.onResume();
        Log.i(TAG, "The activity state—->onResume");
    }

    protected void onPause(){
        super.onPause();
        Log.i(TAG, "The activity state—->onPause");
    }

    protected void onStop(){
        super.onStop();
        Log.i(TAG, "The activity state—->onStop");
    }

    protected void onDestroy(){
        super.onDestroy();
        Log.i(TAG, "The activity state—->onDestroy");
    }

   
}


演示sundy留的小作業 截取LOG

這是演示的結果


//利用LOG展示activity的生命周期 
//註釋表示 中間執行的操作 為方便的觀察數據,可以在LOGCAT窗口(沒有的話可以在window菜單中的show view中調出)的右側單擊加號創建一個過濾器,我的例子中過濾的是demo

//開始運行demo
07-22 11:18:19.311: INFO/demo(281): The activity state—->onStart
07-22 11:18:19.311: INFO/demo(281): The activity state—->onResume

//按下瞭back鍵 返回 activity從stack中彈出
07-22 11:18:34.821: INFO/demo(281): The activity state—->onPause
07-22 11:18:35.090: INFO/demo(281): The activity state—->onStop
07-22 11:18:35.090: INFO/demo(281): The activity state—->onDestroy

//再次啟動demo
07-22 11:18:45.550: INFO/demo(281): The activity state—->onStart
07-22 11:18:45.550: INFO/demo(281): The activity state—->onResume

//按下瞭HOME鍵 當前TASK 處於後臺轉態,系統保存狀態
07-22 11:18:53.750: INFO/demo(281): The activity state—->onPause
07-22 11:18:54.820: INFO/demo(281): The activity state—->onStop

//再次啟動demo 回復原來的TASK activity在棧頂
07-22 11:19:03.550: INFO/demo(281): The activity state—->onReatart
07-22 11:19:03.550: INFO/demo(281): The activity state—->onStart
07-22 11:19:03.550: INFO/demo(281): The activity state—->onResume

You May Also Like