LiveData

public abstract class LiveData
extends Object

java.lang.Object
   ↳ androidx.lifecycle.LiveData<T>


LiveData is a data holder class that can be observed within a given lifecycle. This means that an Observer can be added in a pair with a LifecycleOwner, and this observer will be notified about modifications of the wrapped data only if the paired LifecycleOwner is in active state. LifecycleOwner is considered as active, if its state is Lifecycle.State.STARTED or Lifecycle.State.RESUMED. An observer added via observeForever(Observer) is considered as always active and thus will be always notified about modifications. For those observers, you should manually call removeObserver(Observer).

An observer added with a Lifecycle will be automatically removed if the corresponding Lifecycle moves to Lifecycle.State.DESTROYED state. This is especially useful for activities and fragments where they can safely observe LiveData and not worry about leaks: they will be instantly unsubscribed when they are destroyed.

In addition, LiveData has onActive() and onInactive() methods to get notified when number of active Observers change between 0 and 1. This allows LiveData to release any heavy resources when it does not have any Observers that are actively observing.

This class is designed to hold individual data fields of ViewModel, but can also be used for sharing data between different modules in your application in a decoupled fashion.

See also:

Summary

Public constructors

LiveData(T value)

Creates a LiveData initialized with the given value.

LiveData()

Creates a LiveData with no value assigned to it.

Public methods

T getValue()

Returns the current value.

boolean hasActiveObservers()

Returns true if this LiveData has active observers.

boolean hasObservers()

Returns true if this LiveData has observers.

void observe(LifecycleOwner owner, Observer<? super T> observer)

Adds the given observer to the observers list within the lifespan of the given owner.

void observeForever(Observer<? super T> observer)

Adds the given observer to the observers list.

void removeObserver(Observer<? super T> observer)

Removes the given observer from the observers list.

void removeObservers(LifecycleOwner owner)

Removes all observers that are tied to the given LifecycleOwner.

Protected methods

void onActive()

Called when the number of active observers change from 0 to 1.

void onInactive()

Called when the number of active observers change from 1 to 0.

void postValue(T value)

Posts a task to a main thread to set the given value.

void setValue(T value)

Sets the value.

Inherited methods

Public constructors

LiveData

public LiveData (T value)

Creates a LiveData initialized with the given value.

Parameters
value T: initial value

LiveData

public LiveData ()

Creates a LiveData with no value assigned to it.

Public methods

getValue

public T getValue ()

Returns the current value. Note that calling this method on a background thread does not guarantee that the latest value set will be received.

Returns
T the current value

hasActiveObservers

public boolean hasActiveObservers ()

Returns true if this LiveData has active observers.

Returns
boolean true if this LiveData has active observers

hasObservers

public boolean hasObservers ()

Returns true if this LiveData has observers.

Returns
boolean true if this LiveData has observers

observe

public void observe (LifecycleOwner owner,