android 使用ActivityLifecycleCallbacks管理activity

  • Application通过此接口提供了一套回调方法,用于让开发者对Activity的生命周期事件进行集中处理。
  • public class BaseApplication extends Application {
    
        private static final String TAG = "BaseApplication";
    
        private static BaseApplication sInstance;
    
        public static BaseApplication getInstance() {
            return sInstance;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            sInstance = this;
            registerActivityLifecycleCallbacks(mCallbacks);
        }
    
        private ActivityLifecycleCallbacks mCallbacks = new ActivityLifecycleCallbacks() {
    
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                Log.d(TAG, "onActivityCreated() called with: activity = [" + activity + "], savedInstanceState = [" + savedInstanceState + "]");
            }
    
            @Override
            public void onActivityStarted(Activity activity) {
                Log.d(TAG, "onActivityStarted() called with: activity = [" + activity + "]");
            }
    
            @Override
            public void onActivityResumed(Activity activity) {
                Log.d(TAG, "onActivityResumed() called with: activity = [" + activity + "]");
            }
    
            @Override
            public void onActivityPaused(Activity activity) {
                Log.d(TAG, "onActivityPaused() called with: activity = [" + activity + "]");
            }
    
            @Override
            public void onActivityStopped(Activity activity) {
                Log.d(TAG, "onActivityStopped() called with: activity = [" + activity + "]");
            }
    
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                Log.d(TAG, "onActivitySaveInstanceState() called with: activity = [" + activity + "], outState = [" + outState + "]");
            }
    
            @Override
            public void onActivityDestroyed(Activity activity) {
                Log.d(TAG, "onActivityDestroyed() called with: activity = [" + activity + "]");
            }
        };
    }


你可能感兴趣的:(android-开发)