// Lifecycles only (without ViewModel or LiveData) implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
/** * If a class implements both this interface and {@link LifecycleEventObserver}, then * methods of {@code DefaultLifecycleObserver} will be called first, and then followed by the call * of {@link LifecycleEventObserver#onStateChanged(LifecycleOwner, Lifecycle.Event)} * <p> * * 如果一个类同时继承了该类【DefaultLifecycleObserver】和`LifecycleEventObserver`两个接口,那么该类下的方法会先调用, * 然后`LifecycleEventObserver`下的`onStateChanged(LifecycleOwner, Lifecycle.Event)}`方法会被调用。 * If a class implements this interface and in the same time uses {@link OnLifecycleEvent}, then * annotations will be ignored. */ publicinterfaceDefaultLifecycleObserverextendsFullLifecycleObserver{ @Override defaultvoidonCreate(@NonNull LifecycleOwner owner){ .. } @Override defaultvoidonStart(@NonNull LifecycleOwner owner){ .. } ... }
if (previous != null) { return; } LifecycleOwner lifecycleOwner = mLifecycleOwner.get(); if (lifecycleOwner == null) { // it is null we should be destroyed. Fallback quickly return; }
boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent; ---------------------------calculateTargetStates()---------------------------------------------------------- State targetState = calculateTargetState(observer); mAddingObserverCounter++; 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--; }
@Override publicvoidonStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event){ switch (event) { case ON_CREATE: mFullLifecycleObserver.onCreate(source); break; case ON_START: mFullLifecycleObserver.onStart(source); break; case ON_RESUME: mFullLifecycleObserver.onResume(source); break; case ON_PAUSE: mFullLifecycleObserver.onPause(source); break; case ON_STOP: mFullLifecycleObserver.onStop(source); break; case ON_DESTROY: mFullLifecycleObserver.onDestroy(source); break; case ON_ANY: thrownew IllegalArgumentException("ON_ANY must not been send by anybody"); } if (mLifecycleEventObserver != null) { mLifecycleEventObserver.onStateChanged(source, event); } }
publicstaticvoidinjectIfNeededIn(Activity activity){ if (Build.VERSION.SDK_INT >= 29) { // On API 29+, we can register for the correct Lifecycle callbacks directly 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) { manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit(); // Hopefully, we are the first to make a transaction. manager.executePendingTransactions(); } }
publicvoidhandleLifecycleEvent(@NonNull Lifecycle.Event event){ State next = getStateAfter(event); moveToState(next); }
privatevoidmoveToState(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; }
// happens only on the top of stack (never in reentrance), // so it doesn't have to take in account parents privatevoidsync(){ LifecycleOwner lifecycleOwner = mLifecycleOwner.get(); if (lifecycleOwner == null) { thrownew 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. if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) { backwardPass(lifecycleOwner); } Entry<LifecycleObserver, ObserverWithState> newest = mObserverMap.newest(); if (!mNewEventOccurred && newest != null && mState.compareTo(newest.getValue().mState) > 0) { forwardPass(lifecycleOwner); } } mNewEventOccurred = false; }
isSynced
1 2 3 4 5 6 7 8
privatebooleanisSynced(){ if (mObserverMap.size() == 0) { returntrue; } State eldestObserverState = mObserverMap.eldest().getValue().mState; State newestObserverState = mObserverMap.newest().getValue().mState; return eldestObserverState == newestObserverState && mState == newestObserverState; }