Package icyllis.modernui.lifecycle

# Class Lifecycle

java.lang.Object
icyllis.modernui.lifecycle.Lifecycle

Direct Known Subclasses:
  `LifecycleRegistry`

public abstract class Lifecycle extends Object
Defines an object that has a Lifecycle. `Fragment` class implement `LifecycleOwner` interface which has the `LifecycleOwner.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. This gives you certain guarantees on which state the owner is in.

Observe lifecycle events with `LifecycleObserver`.

     class TestObserver implements LifecycleObserver {
         @Override
         public void onCreate(@Nonnull LifecycleOwner owner) {
             // your code
         }
     }

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static enum`
  `Lifecycle.Event`
  `static enum`
  `Lifecycle.State`
  Lifecycle states.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Lifecycle()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `abstract void`
  `addObserver(LifecycleObserver observer)`
  Adds a LifecycleObserver that will be notified when the LifecycleOwner changes state.
  `abstract Lifecycle.State`
  `getCurrentState()`
  Returns the current state of the Lifecycle.
  `abstract void`
  `removeObserver(LifecycleObserver observer)`
  Removes the given observer from the observers list.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Lifecycle

    public Lifecycle()

- ## Method Details

  - ### addObserver

    @UiThread public abstract void addObserver(@Nonnull LifecycleObserver observer)
    Adds a LifecycleObserver that will be notified when the LifecycleOwner changes state.

    The given observer will be brought to the current state of the LifecycleOwner. For example, if the LifecycleOwner is in `Lifecycle.State.STARTED` state, the given observer will receive `Lifecycle.Event.ON_CREATE`, `Lifecycle.Event.ON_START` events.

    Parameters:
      `observer` - The observer to notify.

  - ### removeObserver

    @UiThread public abstract void removeObserver(@Nonnull LifecycleObserver observer)
    Removes the given observer from the observers list.

    If this method is called while a state change is being dispatched,

    - If the given observer has not yet received that event, it will not receive it.
    - If the given observer has more than 1 method that observes the currently dispatched event and at least one of them received the event, all of them will receive the event and the removal will happen afterwards.
    Parameters:
      `observer` - The observer to be removed.

  - ### getCurrentState

    @UiThread @Nonnull public abstract Lifecycle.State getCurrentState()
    Returns the current state of the Lifecycle.
    Returns:
      The current state of the Lifecycle.