Android -- ActivityLifeCycleCallbacks

ActivityLifeCycleCallbacks

Application經過此接口提供了一套回調方法,用於讓開發者對Activity的生命週期事件進行集中處理。java

爲何用ActivityLifecycleCallbacks

以往若需監測Activity的生命週期事件代碼,你多是這樣作的,重寫每個Acivity的onResume(),而後做統計和處理:api

@Override  
protected void onResume() {  
  super.onResume();  
  //TODO 處理和統計代碼  
  Log.v(TAG, "onResume");  
  Logger.v(TAG, "onResume");  
  Logging.v(TAG, "onResume");  
  //...  
}

ActivityLifecycleCallbacks接口回調能夠簡化這一繁瑣過程,在一個類中做統一處理。ide

ActivityLifecycleCallbacks怎麼用?

在自定義的Application中的onCreate()方法,或在Application的無參構造方法內,調用Application.registerActivityLifecycleCallbacks()方法,並實現ActivityLifecycleCallbacks接口:this

public void onCreate() {  
  super.onCreate();  
  this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {  
   
    @Override  
    public void onActivityStopped(Activity activity) {  
        Logger.v(activity, "onActivityStopped");  
    }  
   
    @Override  
    public void onActivityStarted(Activity activity) {  
        Logger.v(activity, "onActivityStarted");  
    }  
   
    @Override  
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {  
        Logger.v(activity, "onActivitySaveInstanceState");  
    }  
   
    @Override  
    public void onActivityResumed(Activity activity) {  
        Logger.v(activity, "onActivityResumed");  
    }  
   
    @Override  
    public void onActivityPaused(Activity activity) {  
        Logger.v(activity, "onActivityPaused");  
    }  
   
    @Override  
    public void onActivityDestroyed(Activity activity) {  
        Logger.v(activity, "onActivityDestroyed");  
    }  
   
    @Override  
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {  
        Logger.v(activity, "onActivityCreated");  
    }  
  });  
};

Log:code

V/OneActivity: onActivityCreated  
V/OneActivity: onActivityStarted  
V/OneActivity: onActivityResumed  
V/OneActivity: onActivityPaused  
V/MainActivity: onActivityCreated  
V/MainActivity: onActivityStarted  
V/MainActivity: onActivityResumed  
V/OneActivity: onActivityStopped  
V/OneActivity: onActivityDestroyed  
V/MainActivity: onActivityPaused  
V/MainActivity: onActivityStopped  
V/MainActivity: onActivityDestroyed

注意

ActivityLifeCycleCallbacks在API>=14上才能夠調用。接口

兼容API<14

  • 本身寫一個ActivityLifeCycleCallbacks接口:
public interface ActivityLifecycleCallbacks{
 public void onActivityStopped(Activity activity);
 public void onActivityStarted(Activity activity);
 public void onActivitySaveInstanceState(Activity activity, Bundle outState);
 public void onActivityResumed(Activity activity);
 public void onActivityPaused(Activity activity);
 public void onActivityDestroyed(Activity activity);
 public void onActivityCreated(Activity activity, Bundle savedInstanceState);
}
  • 在Application中實現接口:
public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{
 @Override
 public void onCreate() {
  super.onCreate();   
 }
 @Override
 public void onActivityStopped(Activity activity) {
  Log.i("Tracking Activity Stopped", activity.getLocalClassName());
 }
 @Override
 public void onActivityStarted(Activity activity) {
  Log.i("Tracking Activity Started", activity.getLocalClassName());
 }
 @Override
 public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
  Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName());
 }
 @Override
 public void onActivityResumed(Activity activity) {
  Log.i("Tracking Activity Resumed", activity.getLocalClassName());
 }
 @Override
 public void onActivityPaused(Activity activity) {
  Log.i("Tracking Activity Paused", activity.getLocalClassName());
 }
 @Override
 public void onActivityDestroyed(Activity activity) {
  Log.i("Tracking Activity Destroyed", activity.getLocalClassName());
 }
 @Override
 public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
  Log.i("Tracking Activity Created", activity.getLocalClassName());
 }
}
  • 寫基類:
public class BaseActivity extends Activity {
 protected MyApplication nMyApplication;
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  nMyApplication = (MyApplication) getApplication();
  nMyApplication.onActivityCreated(this, savedInstanceState);
 }
 protected void onResume() {
  // TODO Auto-generated method stub
  nMyApplication.onActivityResumed(this);
  super.onResume();
 }
 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  nMyApplication.onActivityPaused(this);
  super.onPause();
 }
 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  nMyApplication.onActivityDestroyed(this);
  super.onDestroy();
 }
 @Override
 protected void onStart() {
  nMyApplication.onActivityStarted(this);
  super.onStart();
 }
 @Override
 protected void onStop() {
  nMyApplication.onActivityStopped(this);
  super.onStop();
 }
 @Override
 protected void onSaveInstanceState(Bundle outState) {
  nMyApplication.onActivitySaveInstanceState(this, outState);
  super.onSaveInstanceState(outState);
 } 
}
  • 全部的類集成這個基類:
public class MainActivity extends BaseActivity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }
}

我是天王蓋地虎的分割線

相關文章
相關標籤/搜索