AcitivityLifecycleCallbacks接口

在应用中我经常需要管理所有的activity,通常的办法是用一个类的static方法来管理,当这些acitivity创建的时候add,消除的时候remove,那么有没有一种更加优雅的方法来处理呢,我们可以使用ActivityLifeCycleCallbacks接口来实现,这个接口中定义的方法对应activity的生命周期的几个方法。

package com.motiongear.simplecc.shademotion;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by simplecc on 2017/6/18.
 */

public class BaseApplication extends Application {

    private static final String TAG = BaseApplication.class.getSimpleName();
    private List<Activity> mCallbacks = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        this.registerActivityLifecycleCallbacks(callbacks);
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
        this.unregisterActivityLifecycleCallbacks(callbacks);
    }

    private Application.ActivityLifecycleCallbacks callbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            Log.d(TAG, "onActivityCreated");
            mCallbacks.add(activity);
        }

        @Override
        public void onActivityStarted(Activity activity) {
            Log.d(TAG, "onActivityStarted");

        }

        @Override
        public void onActivityResumed(Activity activity) {
            Log.d(TAG, "onActiivtyResumed");
        }

        @Override
        public void onActivityPaused(Activity activity) {
            Log.d(TAG, "onActivityPaused");
        }

        @Override
        public void onActivityStopped(Activity activity) {
            Log.d(TAG, "onActivityStopped");
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            Log.d(TAG, "onActivitySaveInstanceState");
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            Log.d(TAG, "onAcitivityDestoryed");
            mCallbacks.remove(activity);
        }
    };
}

代码比较简单就不细讲了,我们来看看这个调用的流程是怎么来的。

Application.java
    ...
    private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
            new ArrayList<ActivityLifecycleCallbacks>();
                     ...
                     ...
  public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        synchronized (mActivityLifecycleCallbacks) {
            mActivityLifecycleCallbacks.add(callback);
        }
    }

在Application的源码中,可以看到有一个mActivityLifecyclerCallbacks的成员变量,它的类型是ArrayList,用来存储ActivityLifeCycleCallbacks接口类型的实例。

Applcation.java

/* package */ void dispatchActivityCreated(Activity activity, Bundle savedInstanceState) {
        Object[] callbacks = collectActivityLifecycleCallbacks();
        if (callbacks != null) {
            for (int i=0; i<callbacks.length; i++) {
                ((ActivityLifecycleCallbacks)callbacks[i]).onActivityCreated(activity,
                        savedInstanceState);
            }
        }
    }

    /* package */ void dispatchActivityStarted(Activity activity) {
       ...
    }

 /* package */ void dispatchActivityResumed(Activity activity) {
      ...
    }

    /* package */ void dispatchActivityPaused(Activity activity) {
       ...
    }

    /* package */ void dispatchActivityStopped(Activity activity) {
      ...
    }

    /* package */ void dispatchActivitySaveInstanceState(Activity activity, Bundle outState) {
      ...
    }

    /* package */ void dispatchActivityDestroyed(Activity activity) {
      ...
    }
    ...
    private Object[] collectActivityLifecycleCallbacks() {
        Object[] callbacks = null;
        synchronized (mActivityLifecycleCallbacks) {
            if (mActivityLifecycleCallbacks.size() > 0) {
                callbacks = mActivityLifecycleCallbacks.toArray();
            }
        }
        return callbacks;
    }

dispatchActivityXXX方法封装了对mActivityLifeCallbacks接口的调用,调用这些方法就会调用接口中对应的方法。

Activity.java

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        if (savedInstanceState != null) {
            Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
            mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
                    ? mLastNonConfigurationInstances.fragments : null);
        }
        mFragments.dispatchCreate();
        getApplication().dispatchActivityCreated(this, savedInstanceState);
        if (mVoiceInteractor != null) {
            mVoiceInteractor.attachActivity(this);
        }
        mCalled = true;
    }

   protected void onStart() {
     ...
        getApplication().dispatchActivityStarted(this);
    }

在Activity的源码中可以看到,在调用activity的各自的生命周期中的方法的时候就会调用这些dispatchXXX方法。

posted @ 2017-06-18 01:53  豌豆豆  阅读(253)  评论(0编辑  收藏  举报