Android Activity 生命周期


Activity其實是繼承了ApplicationContext這個類別,我們可以重寫以下方法,如下代碼:

 public class Activity extends ApplicationContext {
        protected void onCreate(Bundle savedInstanceState);        
        protected void onStart();           
        protected void onRestart();        
        protected void onResume();       
        protected void onPause();        
        protected void onStop();        
        protected void onDestroy();
}
透過底下此程式,即可了解Activity 運作生命周期
 public class Activity extends ApplicationContext {
     package com.tutor.activitydemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class ActivityDemo extends Activity {
   
 private static final String TAG = "ActivityDemo";
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Log.e(TAG, "start onCreate~~~");
    }
    
    @Override
    protected void onStart() {
     super.onStart();
     Log.e(TAG, "start onStart~~~");
    }
    
    @Override
    protected void onRestart() {
     super.onRestart();
     Log.e(TAG, "start onRestart~~~");
    }
    
    @Override
    protected void onResume() {
     super.onResume();
     Log.e(TAG, "start onResume~~~");
    }
    
    @Override
    protected void onPause() {
     super.onPause();
     Log.e(TAG, "start onPause~~~");
    }
    
    @Override
    protected void onStop() {
     super.onStop();
     Log.e(TAG, "start onStop~~~");
    }
    
    @Override
    protected void onDestroy() {
     super.onDestroy();
     Log.e(TAG, "start onDestroy~~~");
    }
    
}

By Calvin Huo on 2013年11月28日 星期四 | | A comment?
0 responses to “Android Activity 生命周期”

Leave a Reply