Lifecycle的生命周期状态事件和状态
Lifecycle使用两个枚举来跟踪关联组件的生命周期状态,这两个枚举类分别是Event和State,State代表Lifecycle的生命周期所处的状态,Event代表Lifecycle生命周期所处的事件。
@SuppressWarnings("WeakerAccess")public enum Event { /** * Constant for onCreate event of the {@link LifecycleOwner}. */ ON_CREATE, /** * Constant for onStart event of the {@link LifecycleOwner}. */ ON_START, /** * Constant for onResume event of the {@link LifecycleOwner}. */ ON_RESUME, /** * Constant for onPause event of the {@link LifecycleOwner}. */ ON_PAUSE, /** * Constant for onStop event of the {@link LifecycleOwner}. */ ON_STOP, /** * Constant for onDestroy event of the {@link LifecycleOwner}. */ ON_DESTROY, /** * An {@link Event Event} constant that can be used to match all events. */ ON_ANY}
@SuppressWarnings("WeakerAccess")public enum State { /** * 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} */ public boolean isAtLeast(@NonNull State state) { return compareTo(state) >= 0; }}Lifeccle是如何观察Activity和Fragment的生命周期在Android 26.1.0版本后的library中Activity和fragment已经默认实现了LifecyclerOwner接口
LifecycleOwner 和 LifecycleRegisterOwner 是继承关系,
LifecycleRegisterOwner extends LifecycleOwner ,不同的是 LifecycleRegisterOwner 的 getLifecycle()方法获取的是 LifecycleRegister,而
LifecycleOwner 的 getLifecycle()方法获取的是 LifecycleActivity
public class ComponentActivity extends androidx.core.app.ComponentActivity implements LifecycleOwner
@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSavedStateRegistryController.performRestore(savedInstanceState); ReportFragment.injectIfNeededIn(this); if (mContentLayoutId != 0) { setContentView(mContentLayoutId); }}
在Activity中,生命周期的监听操作都是在 ReportFragment 中完成的
Fragment
public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener, LifecycleOwner
在Fragment 中,生命周期的监听操作是在
final class FragmentManagerImpl extends FragmentManager
中完成的