0.前言

在日常开发中,监听Activity和Fragment的生命周期,并在相应的生命周期下面做一些处理,是比较常见的需求,Lifecycle的出现为我们提供了一种统一的监听方式,而且Lifecycle在Jetpack的很多插件中都有被使用,比如LiveData、ViewModel等。接下来会从Lifecycle的使用到源码实现,一步步的了解Lifecycle。

1.使用

在androidx包下,androidx.activity.ComponentActivity和androidx.fragment.app.Fragment都实现了LifecycleOwner,表明可以提供Lifecycle实例,相关的源码如下:

public class ComponentActivity extends androidx.core.app.ComponentActivity implements
        LifecycleOwner,
        ViewModelStoreOwner,
        SavedStateRegistryOwner,
        OnBackPressedDispatcherOwner {}


public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener, LifecycleOwner,
        ViewModelStoreOwner, SavedStateRegistryOwner {}


public interface LifecycleOwner {
    @NonNull
    Lifecycle getLifecycle();
}

所以只要是这两个类的子类,就可以直接获得Lifecycle的实例,然后通过addObserver()方法添加订阅者。addObserver()方法定义如下:

    @MainThread
    public abstract void addObserver(@NonNull LifecycleObserver observer);

在addObserver方法中,需要传的参数类型是LifecycleObserver,这是一个标记型的接口,没有任何的方法和属性,那么要怎么用呢?其实,我们真正要看的是他的子类,一个是FullLifecycleObserver,还有一个是LifecycleEventObserver,两个都是继承自LifecycleObserver的接口,源码如下:

interface FullLifecycleObserver extends LifecycleObserver {

    void onCreate(LifecycleOwner owner);

    void onStart(LifecycleOwner owner);

    void onResume(LifecycleOwner owner);

    void onPause(LifecycleOwner owner);

    void onStop(LifecycleOwner owner);

    void onDestroy(LifecycleOwner owner);
}

public interface LifecycleEventObserver extends LifecycleObserver {
    /**
     * Called when a state transition event happens.
     *
     * @param source The source of the event
     * @param event The event
     */
    void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event);
}

所以常用方式是继承这两个类中的一个,并实现方法。
如果继承FullLifecycleObserver,几个实现方法和生命周期一样的名字,一看就明白,而方法的参数LifecycleOwner表示的是Activity或者Fragment。另外,如果是Java8以上的版本,还可以使用DefaultLifecycleObserver这个接口,这是FullLifecycleObserver的子类,并对每一个方法都做了空实现,使用他时只需要实现你想要的方法,而不是所有的方法都必须实现,这种接口方法默认实现是要在Java8才有的特性。
如果继承LifecycleEventObserver,需要实现方法onStateChanged,方法里面的LifecycleOwner参数表示的是Activity或者Fragment,Lifecycle.Event参数是一个枚举类,表示的是生命周期的事件,和生命周期是对应的。
除了使用者两种方式外,还可以直接继承LifecycleObserver,添加方法并用@OnLifecycleEvent注解修饰,示例代码如下:

class MyLifecycleObserver : LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume(lifecycleOwner: LifecycleOwner) {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause(lifecycleOwner: LifecycleOwner) {
    }
    
    //......其他方法
}

这种方式不推荐使用。

以上,就是Lifecycle的简单使用方式。

2.具体实现

上文提到,androidx下的androidx.activity.ComponentActivity和androidx.fragment.app.Fragment实现了LifecycleOwner,可以直接通过getLifecycle()方法获取到Lifecycle实例,其实这个Lifecycle实例是LifecycleRegistry对象,LifecycleRegistry继承自Lifecycle,相关代码如下:

//ComponentActivity
    private final LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
        public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }


    //Fragment
        LifecycleRegistry mLifecycleRegistry;
        private void initLifecycle() {
            mLifecycleRegistry = new LifecycleRegistry(this);
        }
        public Lifecycle getLifecycle() {
            return mLifecycleRegistry;
        }

//LifecycleRegistry
        public class LifecycleRegistry extends Lifecycle {
        }

先看看在Activity和Fragment中,是怎么将生命周期发送给Lifecycle的。

Activity处理流程

在ComponentActivity的onCreate方法中,把生命周期的处理全交给ReportFragment,代码如下:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSavedStateRegistryController.performRestore(savedInstanceState);
        ReportFragment.injectIfNeededIn(this);//具体的处理
        if (mContentLayoutId != 0) {
            setContentView(mContentLayoutId);
        }
    }

可以看到具体的逻辑实在ReportFragment.injectIfNeededIn()方法中,源码如下:

    public static void injectIfNeededIn(Activity activity) {
        //API29及其以上,监听activity生命周期的逻辑
        if (Build.VERSION.SDK_INT >= 29) {
            // On API 29+, we can register for the correct Lifecycle callbacks directly
            ReportFragment.LifecycleCallbacks.registerIn(activity);
        }

        //API29以下,通过在activity添加一个空UI的Fragment,然后监听Fragment的生命周期就是Activity的生命周期
        // Prior to API 29 and to maintain compatibility with older versions of
        // ProcessLifecycleOwner (which may not be updated when lifecycle-runtime is updated and
        // need to support activities that don't extend from FragmentActivity from support lib),
        // use a framework fragment to get the correct timing of Lifecycle events
        android.app.FragmentManager manager = activity.getFragmentManager();
        if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
            manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
            // Hopefully, we are the first to make a transaction.
            manager.executePendingTransactions();
        }
    }

先看下API29及其以上的逻辑,直接上源码:

    @RequiresApi(29)
    static class LifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
        
        static void registerIn(Activity activity) {
            //通过activity提供的接口,监听生命周期,是在API29才加入的接口
            activity.registerActivityLifecycleCallbacks(new ReportFragment.LifecycleCallbacks());
        }

        @Override
        public void onActivityPostCreated(@NonNull Activity activity,
                                          @Nullable Bundle savedInstanceState) {
            //监听到相应的生命周期,分发事件
            dispatch(activity, Lifecycle.Event.ON_CREATE);
        }

        //......其他事件

        @Override
        public void onActivityPrePaused(@NonNull Activity activity) {
            dispatch(activity, Lifecycle.Event.ON_PAUSE);
        }
    }

通过API29新增的接口,监听生命周期,然后调用dispatch(@NonNull Activity activity, @NonNull Lifecycle.Event event)方法分发事件。

然后看API29以下的逻辑,给Activity添加了Fragment,所以Activity的生命周期可以通过Fragment的回调方法获得,源码如下:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //ProcessLifecycleOwner使用此接口处理,和本文无关,略过
        dispatchCreate(mProcessListener);

        //分发事件
        dispatch(Lifecycle.Event.ON_CREATE);
    }

    @Override
    public void onStart() {
        super.onStart();
        dispatchStart(mProcessListener);
        dispatch(Lifecycle.Event.ON_START);
    }

    @Override
    public void onResume() {
        super.onResume();
        dispatchResume(mProcessListener);
        dispatch(Lifecycle.Event.ON_RESUME);
    }

    @Override
    public void onPause() {
        super.onPause();
        dispatch(Lifecycle.Event.ON_PAUSE);
    }

    @Override
    public void onStop() {
        super.onStop();
        dispatch(Lifecycle.Event.ON_STOP);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        dispatch(Lifecycle.Event.ON_DESTROY);
        // just want to be sure that we won't leak reference to an activity
        mProcessListener = null;
    }

    private void dispatch(@NonNull Lifecycle.Event event) {
        //判断是否是29以下,避免29及其以上的重复调用
        if (Build.VERSION.SDK_INT < 29) {
            // Only dispatch events from ReportFragment on API levels prior
            // to API 29. On API 29+, this is handled by the ActivityLifecycleCallbacks
            // added in ReportFragment.injectIfNeededIn
            dispatch(getActivity(), event);
        }
    }

可以看到最终的事件处理都会走到dispatch(@NonNull Activity activity, @NonNull Lifecycle.Event event)方法,和API29一样。

接下来看dispatch的处理逻辑,源码如下:

    static void dispatch(@NonNull Activity activity, @NonNull Lifecycle.Event event) {
        if (activity instanceof LifecycleRegistryOwner) {
            ((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);
            return;
        }

        if (activity instanceof LifecycleOwner) {
            Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
            if (lifecycle instanceof LifecycleRegistry) {
                ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
            }
        }
    }

逻辑比较简单,都是调用LifecycleRegistry的handleLifecycleEvent(@NonNull Lifecycle.Event event)方法处理事件,至此,Activity在监听到生命周期变化后,把事件交给LifecycleRegistry处理,具体的处理逻辑稍后再看。

Fragment处理流程

Androidx下的Fragment,和android.app.Fragment没有任何关系,完全是一个新的类,而Fragment的生命周期监听是FragmentManagerImpl实现的,细节也比较复杂,而且和本文Lifecycle没有太大的关系,具体就不展开讲了,只需要知道的是,当生命周期变化时,最终也和Activity一样会走到LifecycleRegistry的handleLifecycleEvent(@NonNull Lifecycle.Event event)方法,部分源码如下:

    void performStart() {
        mChildFragmentManager.noteStateNotSaved();
        mChildFragmentManager.execPendingActions();
        mState = STARTED;
        mCalled = false;
        onStart();
        if (!mCalled) {
            throw new SuperNotCalledException("Fragment " + this
                    + " did not call through to super.onStart()");
        }
        //具体处理逻辑
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
        if (mView != null) {
            mViewLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START);
        }
        mChildFragmentManager.dispatchStart();
    }

所以,不管是Activity还是Fragment的生命周期,都是在变化时交给LifecycleRegistry处理的。

LifecycleRegistry源码

LifecycleRegistry是Lifecycle的实现类,其中主要的方法有addObserver(@NonNull LifecycleObserver observer)、removeObserver(@NonNull LifecycleObserver observer)、State getCurrentState()。

说LifecycleRegistry之前,先说一下kotlin中,枚举类的compareTo()方法,源码如下:

public abstract class Enum<E : Enum<E>>(name: String, ordinal: Int): Comparable<E> {
    public override final fun compareTo(other: E): Int
}

表示的含义是两个枚举的位置相减,举个具体的例子,Lifecycle.State枚举类的定义如下:

    public enum State {
        DESTROYED,

        INITIALIZED,

        CREATED,

        STARTED,

        RESUMED;

        public boolean isAtLeast(@NonNull Lifecycle.State state) {
            return compareTo(state) >= 0;
        }
    }

那么DESTROYED的位置可以看作0,然后依次加1,RESUMED看作4。当调用Lifecycle.State.RESUMED.compareTo(Lifecycle.State.DESTROYED)方法时,就用RESUMED的位置4减去DESTROYED的位置0,得到值为4。而State中的isAtLeast方法表示的含义就是,对象至少要等于参数,或者大于参数,即在参数之后的位置。理解了这个,才能理解下文的代码。

添加观察者

addObserver()方法表示的是添加观察者,下面是LifecycleRegistry的实现:

    public void addObserver(@NonNull LifecycleObserver observer) {
        //确保在主线程运行
        //当mEnforceMainThread为true时,必须是在主线程调用,否则抛出异常
        enforceMainThreadIfNeeded("addObserver");

        //初始化生命周期状态,如果当前状态不是DESTROYED,从INITIALIZED状态开始
        Lifecycle.State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;

        //根据观察者和初始化状态,生成ObserverWithState实例
        //先把所有类型的观察者都转成LifecycleEventObserver类型,具体流程后面再说
        ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);

        //判断观察者是否已经添加到map,如果已经添加,则直接返回
        ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
        if (previous != null) {
            return;
        }

        //获取LifecycleOwner对象实例,一般来说是Activity或者Fragment实例
        //为了避免内存泄露,使用弱引用,如果对象为空,说明已经被回收了,直接返回
        LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
        if (lifecycleOwner == null) {
            // it is null we should be destroyed. Fallback quickly
            return;
        }

        boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;

        //计算目标状态,目的是从初始化状态遍历到当前实际状态,然后把每一个状态的事件分发
        Lifecycle.State targetState = calculateTargetState(observer);
        mAddingObserverCounter++;
        while ((statefulObserver.mState.compareTo(targetState) < 0
                && mObserverMap.contains(observer))) {
            pushParentState(statefulObserver.mState);

            //获取目标状态的后一个事件,比如当前目标状态是CREATED状态,则返回ON_START事件
            final Lifecycle.Event event = Event.upFrom(statefulObserver.mState);
            if (event == null) {
                throw new IllegalStateException("no event up from " + statefulObserver.mState);
            }

            //分发事件
            statefulObserver.dispatchEvent(lifecycleOwner, event);

            popParentState();
            // mState / subling may have been changed recalculate
            targetState = calculateTargetState(observer);
        }

        if (!isReentrance) {
            // we do sync only on the top level.
            sync();
        }
        mAddingObserverCounter--;
    }

关键的事件分发逻辑,会走到ObserverWithState里面的dispatchEvent(LifecycleOwner owner, Event event)方法。

分发事件

除了在添加观察者的时候,会遍历状态并分发事件外,当生命周期发生变化的时候也会主动调用分发事件,即上文提到的handleLifecycleEvent(@NonNull Lifecycle.Event event)方法,而handleLifecycleEvent方法最终又会走到forwardPass(LifecycleOwner lifecycleOwner)方法,forwardPass源码如下:

    private void forwardPass(LifecycleOwner lifecycleOwner) {
        //遍历所有的观察者,分发事件
        Iterator<Map.Entry<LifecycleObserver, LifecycleRegistry.ObserverWithState>> ascendingIterator =
                mObserverMap.iteratorWithAdditions();
        while (ascendingIterator.hasNext() && !mNewEventOccurred) {
            Map.Entry<LifecycleObserver, LifecycleRegistry.ObserverWithState> entry = ascendingIterator.next();
            LifecycleRegistry.ObserverWithState observer = entry.getValue();
            while ((observer.mState.compareTo(mState) < 0 && !mNewEventOccurred
                    && mObserverMap.contains(entry.getKey()))) {
                pushParentState(observer.mState);
                final Lifecycle.Event event = Event.upFrom(observer.mState);
                if (event == null) {
                    throw new IllegalStateException("no event up from " + observer.mState);
                }
                //具体的事件分发逻辑
                observer.dispatchEvent(lifecycleOwner, event);
                popParentState();
            }
        }
    }

最终也是会走到ObserverWithState里面的dispatchEvent(LifecycleOwner owner, Event event)方法。

Lifecycling

上文有两个地方还没有细说,一个是添加观察者的时候,根据观察者和状态生成ObserverWithState实例;还有一个是ObserverWithState里面的dispatchEvent分发事件。

ObserverWithState

ObserverWithState是一个包装类,有两个属性State和LifecycleEventObserver,源码如下:

    static class ObserverWithState {
        Lifecycle.State mState;
        LifecycleEventObserver mLifecycleObserver;

        ObserverWithState(LifecycleObserver observer, Lifecycle.State initialState) {
            //把所有类型的LifecycleObserver都转成LifecycleEventObserver,具体逻辑在Lifecycling实现
            mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);
            mState = initialState;
        }

        void dispatchEvent(LifecycleOwner owner, Lifecycle.Event event) {
            Lifecycle.State newState = event.getTargetState();
            mState = min(mState, newState);
            //通过LifecycleEventObserver的onStateChanged方法,实现事件的分发
            mLifecycleObserver.onStateChanged(owner, event);
            mState = newState;
        }
    }

可以发现,主要的逻辑是把所有类型的LifecycleObserver都转成LifecycleEventObserver,然后就可以都通过 mLifecycleObserver.onStateChanged实现事件分发。

生成LifecycleEventObserver对象

Lifecycling.lifecycleEventObserver方法源码如下:

    static LifecycleEventObserver lifecycleEventObserver(Object object) {
        //先判断观察者是不是LifecycleEventObserver或FullLifecycleObserver的子类,
        //如果是FullLifecycleObserver的子类,返回FullLifecycleObserverAdapter适配器,
        //适配器会在onStateChanged的时候,根据不同的事件,调用他相应的方法
        boolean isLifecycleEventObserver = object instanceof LifecycleEventObserver;
        boolean isFullLifecycleObserver = object instanceof FullLifecycleObserver;
        if (isLifecycleEventObserver && isFullLifecycleObserver) {
            return new FullLifecycleObserverAdapter((FullLifecycleObserver) object,
                    (LifecycleEventObserver) object);
        }
        if (isFullLifecycleObserver) {
            return new FullLifecycleObserverAdapter((FullLifecycleObserver) object, null);
        }

        //如果只是LifecycleEventObserver的子类,直接返回
        if (isLifecycleEventObserver) {
            return (LifecycleEventObserver) object;
        }

        //如果观察者是通过实现LifecycleObserver,并用注解 @OnLifecycleEvent修饰方法这样的方式实现的,
        //就走下面的逻辑,会用反射实现,具体就不展开了
        //因为会用到反射,所以会有性能问题,这就是这种方法不推荐使用的原因
        final Class<?> klass = object.getClass();
        int type = getObserverConstructorType(klass);
        if (type == GENERATED_CALLBACK) {
            List<Constructor<? extends GeneratedAdapter>> constructors =
                    sClassToAdapters.get(klass);
            if (constructors.size() == 1) {
                GeneratedAdapter generatedAdapter = createGeneratedAdapter(
                        constructors.get(0), object);
                return new SingleGeneratedAdapterObserver(generatedAdapter);
            }
            GeneratedAdapter[] adapters = new GeneratedAdapter[constructors.size()];
            for (int i = 0; i < constructors.size(); i++) {
                adapters[i] = createGeneratedAdapter(constructors.get(i), object);
            }
            return new CompositeGeneratedAdaptersObserver(adapters);
        }
        return new ReflectiveGenericLifecycleObserver(object);
    }

Lifecycling的封装和上文提到的,Lifecycle的使用,可以有三种不同的观察者相对应。
有一个疑问是,为什么要提供三种不同的方式,使用其中的一种或者两种不是也可以吗(去掉反射)。

总结

1.androidx下的androidx.activity.ComponentActivity和androidx.fragment.app.Fragment实现了LifecycleOwner接口,提供了Lifecycle的实例,且这个实例是LifecycleRegistry类型;
2.androidx.activity.ComponentActivity下的生命周期监听,分为API29以上和以下两种情况,API29及其以上,使用Activity新加的接口activity.registerActivityLifecycleCallbacks实现;API29以下,通过添加无UI的Fragment,然后在Fragment监听生命周期。而androidx.fragment.app.Fragment是通过FragmentManagerImpl实现的生命周期监听。
3.LifecycleRegistry会将外部传进来的LifecycleObserver对象都包装成LifecycleEventObserver对象,屏蔽了外部LifecycleObserver的差异性(可能是接口,也可能是注解),然后调用LifecycleEventObserver对象的onStateChanged方法来完成事件回调。
4.最后是一个疑问,为什么要提供多种方式的LifecycleObserver?