跳转至

Lifecycle源码分析

Lifecycle

Lifecycle代表生命周期,是一个抽象类,我们经常使用它的子类LifecycleRegistry

Lifecycle提供了三个抽象方法用来添加、删除LifecycleObserver和获取当前Lifecycle状态。

1
2
3
4
5
6
7
@MainThread
public abstract void addObserver(@NonNull LifecycleObserver observer);
@MainThread
public abstract void removeObserver(@NonNull LifecycleObserver observer);
@MainThread
@NonNull
public abstract State getCurrentState();

Lifecycle还提供了两个内部枚举EventState

Event

Event代表生命周期发生变化时发送的事件。

@SuppressWarnings("WeakerAccess")
public enum Event {
    ON_CREATE, //onCreate发送该事件
    ON_START,
    ON_RESUME,
    ON_PAUSE,
    ON_STOP,
    ON_DESTROY,
    ON_ANY
}

State

State代表当前生命周期状态。

public enum State {
     //处于Destoryed状态,Lifecycle将不会发送任何事件
    DESTROYED,
    INITIALIZED,
    CREATED,
    STARTED,
    RESUMED;
     //比较当前状态大于或等于给定状态
    public boolean isAtLeast(@NonNull State state) {
        return compareTo(state) >= 0;
    }
}

LifecycleOwner

LifecycleOwner即生命周期拥有者,是一个接口,只提供了一个获取生命周期的方法。

1
2
3
4
public interface LifecycleOwner {
    @NonNull
    Lifecycle getLifecycle();
}

LifecycleObserver

LifecycleObserver即生命周期的观察者,是一个空接口,里面没有任何方法。

LifecycleEventObserver

LifecycleEventObserver接口提供了onStateChanged方法。当生命周期发生改变会触发onStateChanged方法。

void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event);

FullLifecycleObserver

FullLifecycleObserver接口提供了与ActivityFragment生命周期同名方法,当触发生命周期,会触发同名方法调用。

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);
}

GeneratedAdapterObserver

SingleGeneratedAdapterObserver内部包含一个GeneratedAdapter对象。CompositeGeneratedAdaptersObserver内部包含一个GeneratedAdapter数组,当onStateChanged调用时会调用GeneratedAdaptercallMethods方法。

当我们自定义Observer对象时,会根据@OnLifecycleEvent注解生成对应的GeneratedAdapter子类。

//我们定义的子类
class MyObserver :LifecycleObserver {
    companion object{
        const val TAG = "MyObserver"
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun connectListener(){
        Log.d(TAG, "connectListener: ")
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun disconnectListener(){
        Log.d(TAG, "disconnectListener: ")
    }
}
//生成的GeneratedAdapter
public class MyObserver_LifecycleAdapter implements GeneratedAdapter {
  final MyObserver mReceiver;

  MyObserver_LifecycleAdapter(MyObserver receiver) {
    this.mReceiver = receiver;
  }

  @Override
  public void callMethods(LifecycleOwner owner, Lifecycle.Event event, boolean onAny,
      MethodCallsLogger logger) {
    boolean hasLogger = logger != null;
    if (onAny) {
      return;
    }
    if (event == Lifecycle.Event.ON_RESUME) {
      if (!hasLogger || logger.approveCall("connectListener", 1)) {
        mReceiver.connectListener();
      }
      return;
    }
    if (event == Lifecycle.Event.ON_PAUSE) {
      if (!hasLogger || logger.approveCall("disconnectListener", 1)) {
        mReceiver.disconnectListener();
      }
      return;
    }
  }
}

ReportFragment

ReportFragment负责在生命周期发生变化时,调用LifecycleEventObserveronStateChanged方法。ComponentActivity通过在onCreate方法中调用ReportFragment的静态方法injectIfNeededInReportFragment进行关联。

1
2
3
4
5
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //传入当前的activity
    ReportFragment.injectIfNeededIn(this);
}

ReportFragmentinjectIfNeededIn方法中,会创建一个ReportFragment对象并添加到传入的Activity上。此外,大于29的版本还会调用ActivityregisterActivityLifecycleCallbacks方法。

public static void injectIfNeededIn(Activity activity) {
    if (Build.VERSION.SDK_INT >= 29) {
        // On API 29+, we can register for the correct Lifecycle callbacks directly
        //>=29 Android Q 直接调用activity的registerActivityLifecycleCallbacks方法。
        //registerActivityLifecycleCallbacks方法是29新增的
        activity.registerActivityLifecycleCallbacks(
                new LifecycleCallbacks());
    }
    // 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) {
        //创建ReportFragment 并添加到Fragment中
        manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
        // Hopefully, we are the first to make a transaction.
        manager.executePendingTransactions();
    }
}

dispatch()

ReportFragment的生命周期方法调用时,会调用dispatch方法。当sdk小于29不会调用该方法,而是在LifecycleCallbacks的回调方法中调用它的重载方法。

private void dispatch(@NonNull Lifecycle.Event event) {
    //既然>=29不调用该方法,为什么injectIfNeededIn不直接大于等于29还要添加一个ReportFragment呢?
    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);
    }
}
//LifecycleCallbacks回调方法会直接调用该方法
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) {
            //最终调用observer的dispatch方法
            ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
        }
    }
}

SafeIterableMap

Lifecycle中的所有Observer通过SafeIterableMap来管理的。SafeIterableMap的内部类Entry是一个双向链表。

1
2
3
4
5
6
7
8
9
static class Entry<K, V> implements Map.Entry<K, V> {
    @NonNull
    final K mKey;
    @NonNull
    final V mValue;
    Entry<K, V> mNext; //指向下一个
    Entry<K, V> mPrevious; //指向后一个
    private WeakHashMap<SupportRemove<K, V>, Boolean> mIterators = new WeakHashMap<>();
}

SafeIterableMap内部定义的几个迭代器。

ListIterator是一个抽象类,它的next方法会调用抽象方法forward。有两个子类AscendingIteratorDescendingIterator,分别实现正序遍历和逆序遍历。

private abstract static class ListIterator<K, V> implements Iterator<Map.Entry<K, V>>,SupportRemove<K, V> {
    Entry<K, V> mExpectedEnd;
    Entry<K, V> mNext;
    ListIterator(Entry<K, V> start, Entry<K, V> expectedEnd) {
        this.mExpectedEnd = expectedEnd; //结束点
        this.mNext = start; //起始点
    }
    @Override
    public boolean hasNext() {
        return mNext != null;
    }
    @SuppressWarnings("ReferenceEquality")
    private Entry<K, V> nextNode() {
        if (mNext == mExpectedEnd || mExpectedEnd == null) {
            return null;
        }
        //调用forward方法
        return forward(mNext);
    }
    @Override
    public Map.Entry<K, V> next() {
        Map.Entry<K, V> result = mNext;
        mNext = nextNode();
        return result;
    }   
}

AscendingIterator起始点指向头部,不断向后遍历,DescendingIterator起始点指向尾部,不断向前遍历。

static class AscendingIterator<K, V> extends ListIterator<K, V> {
    AscendingIterator(Entry<K, V> start, Entry<K, V> expectedEnd) {
        super(start, expectedEnd);
    }
    @Override
    Entry<K, V> forward(Entry<K, V> entry) {
        return entry.mNext; //向后遍历
    }
    @Override
    Entry<K, V> backward(Entry<K, V> entry) {
        return entry.mPrevious;
    }
}
private static class DescendingIterator<K, V> extends ListIterator<K, V> {
    DescendingIterator(Entry<K, V> start, Entry<K, V> expectedEnd) {
        super(start, expectedEnd);
    }
    @Override
    Entry<K, V> forward(Entry<K, V> entry) {
        return entry.mPrevious; //向前遍历
    }
    @Override
    Entry<K, V> backward(Entry<K, V> entry) {
        return entry.mNext;
    }
}
public Iterator<Map.Entry<K, V>> descendingIterator() {
    //mEnd作为起始点
    DescendingIterator<K, V> iterator = new DescendingIterator<>(mEnd, mStart);
    mIterators.put(iterator, false);
    return iterator;
}
public Iterator<Map.Entry<K, V>> iterator() {
    //mStart作为起始点
    ListIterator<K, V> iterator = new AscendingIterator<>(mStart, mEnd);
    mIterators.put(iterator, false);
    return iterator;
}
private class IteratorWithAdditions implements Iterator<Map.Entry<K, V>>, SupportRemove<K, V> {
    private Entry<K, V> mCurrent; //当前值
    private boolean mBeforeStart = true;
    IteratorWithAdditions() {
    }
    @SuppressWarnings("ReferenceEquality")
    @Override
    public void supportRemove(@NonNull Entry<K, V> entry) {
        if (entry == mCurrent) {
            mCurrent = mCurrent.mPrevious;
            mBeforeStart = mCurrent == null;
        }
    }
    @Override
    public boolean hasNext() {
        if (mBeforeStart) {
            return mStart != null;
        }
        return mCurrent != null && mCurrent.mNext != null;
    }
    @Override
    public Map.Entry<K, V> next() {
        //mBeforeStart默认是true 
        if (mBeforeStart) {
            //第一次调用next 
            mBeforeStart = false;
            mCurrent = mStart;
        } else {
            mCurrent = mCurrent != null ? mCurrent.mNext : null;
        }
        return mCurrent;
    }
}

LifecycleRegistry

LifecycleRegistryLifecycle的子类,实现了具体的添加、删除LifecycleObserver和处理Event的操作。ComponentActivity就是直接调用LifecycleRegistry的构造函数创建Lifecycle实例。

1
2
3
4
5
//ComponentActivity
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
public Lifecycle getLifecycle() {
    return mLifecycleRegistry;
}

构造函数

//持有LifecycleOwner的一个弱引用避免内存泄露
private State mState;
private final WeakReference<LifecycleOwner> mLifecycleOwner;
//创建FastSafeIterableMap对象存储
private FastSafeIterableMap<LifecycleObserver, ObserverWithState> mObserverMap =
            new FastSafeIterableMap<>();
public LifecycleRegistry(@NonNull LifecycleOwner provider) {
    mLifecycleOwner = new WeakReference<>(provider);
    mState = INITIALIZED; //当前状态为初始化
}

addObserver()

addObserver方法中会把传入的Observer对象包装成一个ObserverWithState对象,并存入到mObserverMap中。并且获取当前的状态targetStateObserver的状态进行比较,如果Observer的状态小于目标状态则会循环分发事件,直到Observer的状态和targetState一致。比如targetStateSTARTED类,Observer会依次分发ON_CREATEON_STARTON_RESUME事件。

@Override
public void addObserver(@NonNull LifecycleObserver observer) {
    //获取初始状态
    State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
    //创建ObserverWithState
    ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
    //添加到FastSafeIterableMap中 
    //如果添加的LifecycleObserver已经存在 则直接返回存在的value
    ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
    //如果已经添加过,则不重复添加直接返回
    if (previous != null) {
        return;
    }
    LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
    if (lifecycleOwner == null) {
        // it is null we should be destroyed. Fallback quickly
        return;
    }
    //正在添加Observer或者正在处理事件
    boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;
    //计算目标状态
    State targetState = calculateTargetState(observer);
    //正在添加的Observer数量
    mAddingObserverCounter++;
    //如果Observer的状态小于目标状态
    while ((statefulObserver.mState.compareTo(targetState) < 0
            && mObserverMap.contains(observer))) {
        pushParentState(statefulObserver.mState);
        //分发事件
        statefulObserver.dispatchEvent(lifecycleOwner, upEvent(statefulObserver.mState));
        popParentState();
        // mState / subling may have been changed recalculate
        targetState = calculateTargetState(observer);
    }
    if (!isReentrance) {
        // we do sync only on the top level.
        sync();
    }
    mAddingObserverCounter--;
}
private State calculateTargetState(LifecycleObserver observer) {
    //获取前一个Entry
    Entry<LifecycleObserver, ObserverWithState> previous = mObserverMap.ceil(observer);
    //如果前一个Entry不为空,返回前一个Entry的状态
    State siblingState = previous != null ? previous.getValue().mState : null;
    //获取parentState
    State parentState = !mParentStates.isEmpty() ? mParentStates.get(mParentStates.size() - 1)
            : null;
    return min(min(mState, siblingState), parentState);
}

ObserverWithState

ObserverWithStateLifecycleRegistry的内部类。通过调用LifecyclinglifecycleEventObserver方法,将传入的Observer对象转换为LifecycleEventObserver对象。并且添加了一个mState字段来保存Observer的状态。

//ObserverWithState是LifecycleRegistry的静态内部类
static class ObserverWithState {
    State mState;
    LifecycleEventObserver mLifecycleObserver;
    ObserverWithState(LifecycleObserver observer, State initialState) {
        //调用Lifecycling的lifecycleEventObserver方法创建一个LifecycleEventObserver对象
        mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);
        mState = initialState;
    }
    void dispatchEvent(LifecycleOwner owner, Event event) {
        State newState = getStateAfter(event);
        mState = min(mState, newState);
        mLifecycleObserver.onStateChanged(owner, event);
        mState = newState;
    }
}

handleLifecycleEvent()

ReportFragment的dispatch方法会调用LifecycleRegistry的handleLifecycleEvent方法来处理Event。

1
2
3
4
5
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
    //获取Event对应的State
    State next = getStateAfter(event);
    moveToState(next);
}
private void moveToState(State next) {
    if (mState == next) {
        return;
    }
    mState = next;
    if (mHandlingEvent || mAddingObserverCounter != 0) {
        mNewEventOccurred = true;
        // we will figure out what to do on upper level.
        return;
    }
    mHandlingEvent = true;
    sync();
    mHandlingEvent = false;
}

handleLifecycleEvent方法最终会调用sync方法来同步所有Observer的状态。在sync中,如果当前State小于链表中第一ObserverState,会调用backwardPass方法后向前同步所有ObserverSate。如果当前状态大于最后一个ObserverState,调用forwardPass从前向后遍历Observer同步State

private void sync() {
    LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
    if (lifecycleOwner == null) {
        throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is already"
                + "garbage collected. It is too late to change lifecycle state.");
    }
    while (!isSynced()) {
        mNewEventOccurred = false;
        // no need to check eldest for nullability, because isSynced does it for us.
        //如果当前State小于链表中第一Observer的State 
        //调用backwardPass方法后向前同步所有Observer的Sate
        if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) {
            backwardPass(lifecycleOwner);
        }
        //获取链表中最后一个Entry
        Entry<LifecycleObserver, ObserverWithState> newest = mObserverMap.newest();
        //如果当前状态大于最后一个Observer的State
        //调用forwardPass从前向后遍历Observer同步State
        if (!mNewEventOccurred && newest != null
                && mState.compareTo(newest.getValue().mState) > 0) {
            forwardPass(lifecycleOwner);
        }
    }
    mNewEventOccurred = false;
}

backwardPass()

private void backwardPass(LifecycleOwner lifecycleOwner) {
    Iterator<Entry<LifecycleObserver, ObserverWithState>> descendingIterator =
            mObserverMap.descendingIterator();
    while (descendingIterator.hasNext() && !mNewEventOccurred) {
        Entry<LifecycleObserver, ObserverWithState> entry = descendingIterator.next();
        //获取对应的observer对象
        ObserverWithState observer = entry.getValue();
        while ((observer.mState.compareTo(mState) > 0 && !mNewEventOccurred
                && mObserverMap.contains(entry.getKey()))) {
            Event event = downEvent(observer.mState);
            pushParentState(getStateAfter(event));
            //调用Observer的dispatchEvent方法
            observer.dispatchEvent(lifecycleOwner, event);
            popParentState();
        }
    }
}
private static Event downEvent(State state) {
    switch (state) {
        case INITIALIZED:
            throw new IllegalArgumentException();
        case CREATED:
            return ON_DESTROY;
        case STARTED:
            return ON_STOP;
        case RESUMED:
            return ON_PAUSE;
        case DESTROYED:
            throw new IllegalArgumentException();
    }
    throw new IllegalArgumentException("Unexpected state value " + state);
}

Lifecycling

lifecycleEventObserver()

lifecycleEventObserver方法负责通过传入LifecycleObserver对象构建一个LifecycleEventObserver对象。

//方法参数为什么不定义为LifecycleObserver??
@NonNull
static LifecycleEventObserver lifecycleEventObserver(Object object) {

    boolean isLifecycleEventObserver = object instanceof LifecycleEventObserver;
    boolean isFullLifecycleObserver = object instanceof FullLifecycleObserver;
    //如果实现了LifecycleEventObserver和FullLifecycleObserver两个接口
    //创建FullLifecycleObserverAdapter
    if (isLifecycleEventObserver && isFullLifecycleObserver) {
        return new FullLifecycleObserverAdapter((FullLifecycleObserver) object,
                (LifecycleEventObserver) object);
    }
    //如果只实现了LifecycleEventObserver创建FullLifecycleObserverAdapter
    //传入的LifecycleEventObserver为null
    if (isFullLifecycleObserver) {
        return new FullLifecycleObserverAdapter((FullLifecycleObserver) object, null);
    }
    //如果只实现了FullLifecycleObserver 直接强转返回
    if (isLifecycleEventObserver) {
        return (LifecycleEventObserver) object;
    }
    final Class<?> klass = object.getClass();
    //获取类型
    int type = getObserverConstructorType(klass);
    if (type == GENERATED_CALLBACK) {
        //从Map中获取构造函数
        List<Constructor<? extends GeneratedAdapter>> constructors =
                sClassToAdapters.get(klass);
        //如果只有一个构造函数 直接返回SingleGeneratedAdapterObserver
        if (constructors.size() == 1) {
            GeneratedAdapter generatedAdapter = createGeneratedAdapter(
                    constructors.get(0), object);
            return new SingleGeneratedAdapterObserver(generatedAdapter);
        }
        //如果有多个构造函数 则创建CompositeGeneratedAdaptersObserver
        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);
    }
    //否则返回ReflectiveGenericLifecycleObserver
    return new ReflectiveGenericLifecycleObserver(object);
}

getObserverConstructorType

private static int getObserverConstructorType(Class<?> klass) {
    //从缓存中取
    Integer callbackCache = sCallbackCache.get(klass);
    if (callbackCache != null) {
        return callbackCache;
    }
    //解析ObserverCallback
    int type = resolveObserverCallbackType(klass);
    sCallbackCache.put(klass, type);
    return type;
}

resolveObserverCallbackType

private static int resolveObserverCallbackType(Class<?> klass) {
    // anonymous class bug:35073837
    if (klass.getCanonicalName() == null) {
        return REFLECTIVE_CALLBACK;
    }
    //获取构造函数
    Constructor<? extends GeneratedAdapter> constructor = generatedConstructor(klass)
    if (constructor != null) {
        //构造函数不为空 存入map中
        sClassToAdapters.put(klass, Collections
                .<Constructor<? extends GeneratedAdapter>>singletonList(constructor))
        return GENERATED_CALLBACK;
    }
    boolean hasLifecycleMethods = ClassesInfoCache.sInstance.hasLifecycleMethods(klas
    if (hasLifecycleMethods) {
        return REFLECTIVE_CALLBACK;
    }
    Class<?> superclass = klass.getSuperclass();
    List<Constructor<? extends GeneratedAdapter>> adapterConstructors = null;
    if (isLifecycleParent(superclass)) {
        if (getObserverConstructorType(superclass) == REFLECTIVE_CALLBACK) {
            return REFLECTIVE_CALLBACK;
        }
        adapterConstructors = new ArrayList<>(sClassToAdapters.get(superclass));
    }
    for (Class<?> intrface : klass.getInterfaces()) {
        if (!isLifecycleParent(intrface)) {
            continue;
        }
        if (getObserverConstructorType(intrface) == REFLECTIVE_CALLBACK) {
            return REFLECTIVE_CALLBACK;
        }
        if (adapterConstructors == null) {
            adapterConstructors = new ArrayList<>();
        }
        adapterConstructors.addAll(sClassToAdapters.get(intrface));
    }
    if (adapterConstructors != null) {
        sClassToAdapters.put(klass, adapterConstructors);
        return GENERATED_CALLBACK;
    }
    return REFLECTIVE_CALLBACK;
}

generatedConstructor()

generatedConstructor方法获取生成的GeneratedAdapter的构造函数。

@Nullable
private static Constructor<? extends GeneratedAdapter> generatedConstructor(Class<?> klass) {
    try {
        Package aPackage = klass.getPackage();
        String name = klass.getCanonicalName();
        //获取包名
        final String fullPackage = aPackage != null ? aPackage.getName() : "";
        //获取adapterName
        final String adapterName = getAdapterName(fullPackage.isEmpty() ? name :
                name.substring(fullPackage.length() + 1));
        //获取Class对象
        @SuppressWarnings("unchecked") final Class<? extends GeneratedAdapter> aClass =
                (Class<? extends GeneratedAdapter>) Class.forName(
                        fullPackage.isEmpty() ? adapterName : fullPackage + "." + adapterName);
        //构造函数
        Constructor<? extends GeneratedAdapter> constructor =
                aClass.getDeclaredConstructor(klass);
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        return constructor;
    } catch (ClassNotFoundException e) {
        return null;
    } catch (NoSuchMethodException e) {
        // this should not happen
        throw new RuntimeException(e);
    }
}

参考