ActivityLifecycleCallbacks使用方法初探

yangxi_001發表於2016-12-20

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怎麼用?

  • android.app.Application.ActivityLifecycleCallbacks
  • 要求API 14+ (Android 4.0+)
  • 繼承Application
    public class BaseApplication extends Application

  • 在AndroidManifest裡起用自定義Application
    <application android:name=".global.BaseApplication"

  • 重寫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");
        }
      });
    };

  • 執行結果(Logcat日誌)

    10-21 14:32:57.722: V/WelcomeActivity(8085): onActivityCreated
    10-21 14:32:57.762: V/WelcomeActivity(8085): onActivityStarted
    10-21 14:32:57.762: V/WelcomeActivity(8085): onActivityResumed
    10-21 14:32:59.164: V/WelcomeActivity(8085): onActivityPaused
    10-21 14:32:59.194: V/MainActivity(8085): onActivityCreated
    10-21 14:32:59.224: V/MainActivity(8085): onActivityStarted
    10-21 14:32:59.224: V/MainActivity(8085): onActivityResumed
    10-21 14:32:59.735: V/WelcomeActivity(8085): onActivityStopped
    10-21 14:32:59.735: V/WelcomeActivity(8085): onActivityDestroyed
    10-21 14:33:06.502: V/MainActivity(8085): onActivityPaused
    10-21 14:33:06.612: V/MainActivity(8085): onActivityStopped
    10-21 14:33:06.612: V/MainActivity(8085): onActivityDestroyed

ActivityLifecycleCallbacks的擴充用法

  • 本次初探僅嘗試使用Log日誌工具作簡要測試,如需滿足較複雜的統計或除錯需求時,此法可能會大大減少插入程式碼量,提高效率

  • API僅在14+版本上提供此介面回撥,Android 4.0以下系統如何使用?

  • API僅針對上述幾個Activity的生命週期事件留出了介面回撥,可能已無法滿足日益過渡為使用Fragment的今日需求, 如何在更大範圍內應用LifecycleCallbacks?