安卓app实现全局监听activity的生命周期

1.在自定义MyApplication 中注册生命周期的方法registerActivityLifecycleCallbacks(new ActivityLifecycleListener(this))

 
  

public class MyApplication extends Application {
    private static UserSPUtil sharedPreferences;
    public static Context context;
    public static final String TAG1 = "ActivityLifecycle";
    public static final String TAG = "token";
    @Override
    public void onCreate() {
        super.onCreate();
       
        //注册生命周期监听器
        registerActivityLifecycleCallbacks(new ActivityLifecycleListener(this));
    

    }
2.自定义ActivityLifecycleListener实现Application.ActivityLifecycleCallbacks接口


public class ActivityLifecycleListener implements Application.ActivityLifecycleCallbacks {

  

  public ActivityLifecycleListener(Context context) {
   
  }

  @Override
  public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
   
   }

  @Override
  public void onActivityStarted(final Activity activity) {
   
   }


  @Override
  public void onActivityResumed(final Activity activity) {


  }


  @Override
  public void onActivityPaused(Activity activity) {
  }

  @Override
  public void onActivityStopped(Activity activity) {

  
  }

  @Override
  public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

  }

  @Override
  public void onActivityDestroyed(Activity activity) {
  }


}

你可能感兴趣的:(安卓开发类)