Package icyllis.modernui.lifecycle

# Class LifecycleRegistry

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

public class LifecycleRegistry extends Lifecycle
An implementation of `Lifecycle` that can handle multiple observers.

It is used by Fragments. You can also directly use it if you have a custom LifecycleOwner.

- ## Nested Class Summary

  ## Nested classes/interfaces inherited from class icyllis.modernui.lifecycle.Lifecycle

  `Lifecycle.Event, Lifecycle.State`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `LifecycleRegistry(LifecycleOwner provider)`
  Creates a new LifecycleRegistry for the given provider.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addObserver(LifecycleObserver observer)`
  Adds a LifecycleObserver that will be notified when the LifecycleOwner changes state.
  `Lifecycle.State`
  `getCurrentState()`
  Returns the current state of the Lifecycle.
  `int`
  `getObserverCount()`
  The number of observers.
  `void`
  `handleLifecycleEvent(Lifecycle.Event event)`
  Sets the current state and notifies the observers.
  `void`
  `removeObserver(LifecycleObserver observer)`
  Removes the given observer from the observers list.
  `void`
  `setCurrentState(Lifecycle.State state)`
  Moves the Lifecycle to the given state and dispatches necessary events to the observers.

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

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

- ## Constructor Details

  - ### LifecycleRegistry

    public LifecycleRegistry(@Nonnull LifecycleOwner provider)
    Creates a new LifecycleRegistry for the given provider.

    You should usually create this inside your LifecycleOwner class's constructor and hold onto the same instance.

    Parameters:
      `provider` - The owner LifecycleOwner

- ## Method Details

  - ### setCurrentState

    @UiThread public void setCurrentState(@Nonnull Lifecycle.State state)
    Moves the Lifecycle to the given state and dispatches necessary events to the observers.
    Parameters:
      `state` - new state

  - ### handleLifecycleEvent

    public void handleLifecycleEvent(@Nonnull Lifecycle.Event event)
    Sets the current state and notifies the observers.

    Note that if the `currentState` is the same state as the last call to this method, calling this method has no effect.

    Parameters:
      `event` - The event that was received

  - ### addObserver

    public void addObserver(@Nonnull LifecycleObserver observer)
    Description copied from class: `Lifecycle`
    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.

    Specified by:
      `addObserver` in class `Lifecycle`
    Parameters:
      `observer` - The observer to notify.

  - ### removeObserver

    public void removeObserver(@Nonnull LifecycleObserver observer)
    Description copied from class: `Lifecycle`
    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.
    Specified by:
      `removeObserver` in class `Lifecycle`
    Parameters:
      `observer` - The observer to be removed.

  - ### getObserverCount

    public int getObserverCount()
    The number of observers.
    Returns:
      The number of observers.

  - ### getCurrentState

    @Nonnull public Lifecycle.State getCurrentState()
    Description copied from class: `Lifecycle`
    Returns the current state of the Lifecycle.
    Specified by:
      `getCurrentState` in class `Lifecycle`
    Returns:
      The current state of the Lifecycle.