=================================================================================================================================== /** * A class that has an Android lifecycle. These events can be used by custom components to * handle lifecycle changes without implementing any code inside the Activity or the Fragment. * * @see Lifecycle * @see ViewTreeLifecycleOwner */ @SuppressWarnings({"WeakerAccess", "unused"}) public interface LifecycleOwner { /** * Returns the Lifecycle of the provider. * * @return The lifecycle of the provider. */ @NonNull Lifecycle getLifecycle(); } =================================================================================================================================== /** * Marks a class as a LifecycleObserver. Don't use this interface directly. Instead implement either * {@link DefaultLifecycleObserver} or {@link LifecycleEventObserver} to be notified about * lifecycle events. * * @see Lifecycle Lifecycle - for samples and usage patterns. */ @SuppressWarnings("WeakerAccess") public interface LifecycleObserver { } =================================================================================================================================== Defines an object that has an Android Lifecycle. Fragment and FragmentActivity classes implement LifecycleOwner interface which has the getLifecycle method to access the Lifecycle. You can also implement LifecycleOwner in your own classes. Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME events in this class are dispatched after the LifecycleOwner's related method returns. Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP, Lifecycle.Event.ON_DESTROY events in this class are dispatched before the LifecycleOwner's related method is called. For instance, Lifecycle.Event.ON_START will be dispatched after onStart returns, Lifecycle.Event.ON_STOP will be dispatched before onStop is called. This gives you certain guarantees on which state the owner is in. To observe lifecycle events call addObserver(LifecycleObserver) passing an object that implements either DefaultLifecycl eObserver or LifecycleEventObserver. public abstract class Lifecycle { @MainThread public abstract void addObserver(@NonNull LifecycleObserver observer); @MainThread public abstract void removeObserver(@NonNull LifecycleObserver observer); @MainThread @NonNull public abstract State getCurrentState(); public enum Event { ON_CREATE, ON_START, ON_RESUME, ON_PAUSE, ON_STOP, ON_DESTROY, ON_ANY; @Nullable public static Event downFrom(@NonNull State state) { switch (state) { case CREATED: return ON_DESTROY; case STARTED: return ON_STOP; case RESUMED: return ON_PAUSE; default: return null; } } @Nullable public static Event downTo(@NonNull State state) { switch (state) { case DESTROYED: return ON_DESTROY; case CREATED: return ON_STOP; case STARTED: return ON_PAUSE; default: return null; } } @Nullable public static Event upFrom(@NonNull State state) { switch (state) { case INITIALIZED: return ON_CREATE; case CREATED: return ON_START; case STARTED: return ON_RESUME; default: return null; } } @Nullable public static Event upTo(@NonNull State state) { switch (state) { case CREATED: return ON_CREATE; case STARTED: return ON_START; case RESUMED: return ON_RESUME; default: return null; } } @NonNull public State getTargetState() { switch (this) { case ON_CREATE: case ON_STOP: return State.CREATED; case ON_START: case ON_PAUSE: return State.STARTED; case ON_RESUME: return State.RESUMED; case ON_DESTROY: return State.DESTROYED; case ON_ANY: break; } throw new IllegalArgumentException(this + " has no target state"); } } public enum State { DESTROYED, INITIALIZED, CREATED, STARTED, RESUMED; public boolean isAtLeast(@NonNull State state) { return compareTo(state) >= 0; } } }