Android -- ActivityLifeCycleCallbacks

ActivityLifeCycleCallbacks

Application通过此接口提供了一套回调方法,用于让开发者对Activity的生命周期事件进行集中处理。

为什么用ActivityLifecycleCallbacks

以往若需监测Activity的生命周期事件代码,你可能是这样做的,重写每一个Acivity的onResume(),然后作统计和处理:

@Override  
protected void onResume() {  
  super.onResume();  
  //TODO 处理和统计代码 
  Log.v(TAG, "onResume");  
  Logger.v(TAG, "onResume");  
  Logging.v(TAG, "onResume");  
  //... 
}  

ActivityLifecycleCallbacks接口回调可以简化这一繁琐过程,在一个类中作统一处理。

ActivityLifecycleCallbacks怎么用?

在自定义的Application中的onCreate()方法,或在Application的无参构造方法内,调用Application.registerActivityLifecycleCallbacks()方法,并实现ActivityLifecycleCallbacks接口:

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:

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);
 }
}

我是天王盖地虎的分割线

你可能感兴趣的:(Android -- ActivityLifeCycleCallbacks)