// Lifecycles only (without ViewModel or LiveData) implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
// 注解处理器: // 1. 添加时则使用注解处理器的方式处理Lifecycle的注解 // 2. 未添加时则使用反射方式处理Lifecycle的注解 // Annotation processor annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // alternately - if using Java8, use the following instead of lifecycle-compiler implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
publicenumState{ /** * Destroyed state for a LifecycleOwner. After this event, this Lifecycle will not dispatch * any more events. For instance, for an {@link android.app.Activity}, this state is reached * <b>right before</b> Activity's {@link android.app.Activity#onDestroy() onDestroy} call. */ DESTROYED,
/** * Initialized state for a LifecycleOwner. For an {@link android.app.Activity}, this is * the state when it is constructed but has not received * {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} yet. */ INITIALIZED,
/** * Created state for a LifecycleOwner. For an {@link android.app.Activity}, this state * is reached in two cases: * <ul> * <li>after {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} call; * <li><b>right before</b> {@link android.app.Activity#onStop() onStop} call. * </ul> */ CREATED,
/** * Started state for a LifecycleOwner. For an {@link android.app.Activity}, this state * is reached in two cases: * <ul> * <li>after {@link android.app.Activity#onStart() onStart} call; * <li><b>right before</b> {@link android.app.Activity#onPause() onPause} call. * </ul> */ STARTED,
/** * Resumed state for a LifecycleOwner. For an {@link android.app.Activity}, this state * is reached after {@link android.app.Activity#onResume() onResume} is called. */ RESUMED;
/** * Compares if this State is greater or equal to the given {@code state}. * * @param state State to compare with * @return true if this State is greater or equal to the given {@code state} */ publicbooleanisAtLeast(@NonNull State state){ return compareTo(state) >= 0; } } }
Lifecycle初始化
1 2 3 4 5 6 7 8 9
publicclassLifecycleRegistryextendsLifecycle{ privatefinal WeakReference<LifecycleOwner> mLifecycleOwner; private State mState;
@MainThread publicvoidsetCurrentState(@NonNull State state){ moveToState(state); }
publicvoidhandleLifecycleEvent(@NonNull Lifecycle.Event event){ State next = getStateAfter(event); moveToState(next); }
static State getStateAfter(Event event){ switch (event) { case ON_CREATE: case ON_STOP: return CREATED; case ON_START: case ON_PAUSE: return STARTED; case ON_RESUME: return RESUMED; case ON_DESTROY: return DESTROYED; case ON_ANY: break; } thrownew IllegalArgumentException("Unexpected event value " + event); }
if (isLifecycleEventObserver) { return (LifecycleEventObserver) object; } // 不是上面的子类,则走反射或注解逻辑,前面的实例便是如此 final Class<?> klass = object.getClass(); // 此处根据klass类去查找是否有注解处理器生成的类 int type = getObserverConstructorType(klass); if (type == GENERATED_CALLBACK) { // 如果注解处理器有起作用,则通过生成的GeneratedAdapter子类构造器创建实例 List<Constructor<? extends GeneratedAdapter>> constructors = sClassToAdapters.get(klass); if (constructors.size() == 1) { GeneratedAdapter generatedAdapter = createGeneratedAdapter(constructors.get(0), object); returnnew SingleGeneratedAdapterObserver(generatedAdapter); } GeneratedAdapter[] adapters = new GeneratedAdapter[constructors.size()]; for (int i = 0; i < constructors.size(); i++) { adapters[i] = createGeneratedAdapter(constructors.get(i), object); } returnnew CompositeGeneratedAdaptersObserver(adapters); } // 使用反射方式 returnnew ReflectiveGenericLifecycleObserver(object); }
GeneratedAdapter
GeneratedAdapter接口如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
publicinterfaceGeneratedAdapter{
/** * Called when a state transition event happens. * * @param source The source of the event * @param event The event * @param onAny approveCall onAny handlers * @param logger if passed, used to track called methods and prevent calling the same method * twice */ voidcallMethods(LifecycleOwner source, Lifecycle.Event event, boolean onAny, MethodCallsLogger logger); }
@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); } } }