Skip to content

Most visited

Recently visited

navigation
added in version 1.0.0
belongs to Maven artifact android.arch.lifecycle:runtime:1.0.3

LifecycleRegistry

public class LifecycleRegistry
extends Lifecycle

java.lang.Object
   ↳ android.arch.lifecycle.Lifecycle
     ↳ android.arch.lifecycle.LifecycleRegistry


An implementation of Lifecycle that can handle multiple observers.

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

Summary

Public constructors

LifecycleRegistry(LifecycleOwner provider)

Creates a new LifecycleRegistry for the given provider.

Public methods

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 markState(Lifecycle.State state)

Moves the Lifecycle to the given state and dispatches necessary events to the observers.

void removeObserver(LifecycleObserver observer)

Removes the given observer from the observers list.

Inherited methods

From class android.arch.lifecycle.Lifecycle
From class java.lang.Object

Public constructors

LifecycleRegistry

added in version 1.0.0
LifecycleRegistry (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 LifecycleOwner: The owner LifecycleOwner

Public methods

addObserver

added in version 1.0.0
void addObserver (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 STARTED state, the given observer will receive ON_CREATE, ON_START events.

Parameters
observer LifecycleObserver: The observer to notify.

getCurrentState

added in version 1.0.0
Lifecycle.State getCurrentState ()

Returns the current state of the Lifecycle.

Returns
Lifecycle.State The current state of the Lifecycle.

getObserverCount

added in version 1.0.0
int getObserverCount ()

The number of observers.

Returns
int The number of observers.

handleLifecycleEvent

added in version 1.0.0
void handleLifecycleEvent (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 Lifecycle.Event: The event that was received

markState

added in version 1.0.0
void markState (Lifecycle.State state)

Moves the Lifecycle to the given state and dispatches necessary events to the observers.

Parameters
state Lifecycle.State: new state

removeObserver

added in version 1.0.0
void removeObserver (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 LifecycleObserver: The observer to be removed.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)