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 + "]");
            }
        };
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值