LiveData

public abstract class LiveData<T>

Known direct subclasses
MutableLiveData

LiveData which publicly exposes setValue and postValue method.

SliceLiveData.CachedSliceLiveData

Implementation of LiveDatathat provides controls over how cached vs live slices work.

Known indirect subclasses
MediatorLiveData

LiveData subclass which may observe other LiveData objects and react on OnChanged events from them.


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 STARTED or RESUMED. An observer added via observeForever is considered as always active and thus will be always notified about modifications. For those observers, you should manually call removeObserver.

An observer added with a Lifecycle will be automatically removed if the corresponding Lifecycle moves to 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.

Parameters
<T>

The type of data held by this instance

See also
ViewModel

Summary

Public constructors

Creates a LiveData with no value assigned to it.

LiveData(T value)

Creates a LiveData initialized with the given value.

Public methods

@Nullable T

Returns the current value.

boolean

Returns true if this LiveData has active observers.

boolean

Returns true if this LiveData has observers.

@MainThread void

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

@MainThread void

Adds the given observer to the observers list.

@MainThread void

Removes the given observer from the observers list.

@MainThread void

Removes all observers that are tied to the given LifecycleOwner.

Extension functions

final @NonNull Flow<@NonNull T>
<T extends Object> FlowLiveDataConversions.asFlow(
    @NonNull LiveData<@NonNull T> receiver
)

Creates a Flow containing values dispatched by originating LiveData: at the start a flow collector receives the latest value held by LiveData and then observes LiveData updates.

final @MainThread @NonNull Observer<@NonNull T>
<T extends Object> LiveDataKt.observe(
    @NonNull LiveData<@NonNull T> receiver,
    @NonNull LifecycleOwner owner,
    @NonNull Function1<@NonNull T, Unit> onChanged
)

This method is deprecated. This extension method is not required when using Kotlin 1.4.

final @NonNull Publisher<@NonNull T>
<T extends Object> LiveDataReactiveSteamsKt.toPublisher(
    @NonNull LiveData<@NonNull T> receiver,
    @NonNull LifecycleOwner lifecycle
)

Adapts the given LiveData stream to a ReactiveStreams Publisher.

final @NonNull LiveData<@NonNull PagingData<@NonNull T>>
<T extends Object> PagingLiveData.cachedIn(
    @NonNull LiveData<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull Lifecycle lifecycle
)

Operator which caches a LiveData of PagingData within the scope of a Lifecycle.

final @NonNull LiveData<@NonNull PagingData<@NonNull T>>
<T extends Object> PagingLiveData.cachedIn(
    @NonNull LiveData<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull ViewModel viewModel
)

Operator which caches a LiveData of PagingData within the scope of a ViewModel.

final @NonNull LiveData<@NonNull PagingData<@NonNull T>>
<T extends Object> PagingLiveData.cachedIn(
    @NonNull LiveData<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull CoroutineScope scope
)

Operator which caches a LiveData of PagingData within a CoroutineScope.

final @NonNull LiveData<@NonNull X>

Creates a new LiveData object does not emit a value until the source this LiveData value has been changed.

final @NonNull LiveData<@NonNull Y>
<X extends Object, Y extends Object> TransformationsKt.map(
    @NonNull LiveData<@NonNull X> receiver,
    @NonNull Function1<@NonNull X, @NonNull Y> transform
)

Returns a LiveData mapped from this LiveData by applying transform to each value set on this LiveData.

final @NonNull LiveData<@NonNull Y>
<X extends Object, Y extends Object> TransformationsKt.switchMap(
    @NonNull LiveData<@NonNull X> receiver,
    @NonNull Function1<@NonNull X, LiveData<@NonNull Y>> transform
)

Returns a LiveData mapped from the input thisLiveData by applying transform to each value set on this.

Public constructors

LiveData

public LiveData()

Creates a LiveData with no value assigned to it.

LiveData

public LiveData(T value)

Creates a LiveData initialized with the given value.

Parameters
T value

initial value

Public methods

getValue

public @NullablegetValue()

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
@Nullable 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

@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<Object> observer)

Adds the given observer to the observers list within the lifespan of the given owner. The events are dispatched on the main thread. If LiveData already has data set, it will be delivered to the observer.

The observer will only receive events if the owner is in STARTED or RESUMED state (active).

If the owner moves to the DESTROYED state, the observer will automatically be removed.

When data changes while the owner is not active, it will not receive any updates. If it becomes active again, it will receive the last available data automatically.

LiveData keeps a strong reference to the observer and the owner as long as the given LifecycleOwner is not destroyed. When it is destroyed, LiveData removes references to the observer &the; owner.

If the given owner is already in DESTROYED state, LiveData ignores the call.

If the given owner, observer tuple is already in the list, the call is ignored. If the observer is already in the list with another owner, LiveData throws an IllegalArgumentException.

Parameters
@NonNull LifecycleOwner owner

The LifecycleOwner which controls the observer

@NonNull Observer<Object> observer

The observer that will receive the events

observeForever

@MainThread
public void observeForever(@NonNull Observer<Object> observer)

Adds the given observer to the observers list. This call is similar to observe with a LifecycleOwner, which is always active. This means that the given observer will receive all events and will never be automatically removed. You should manually call removeObserver to stop observing this LiveData. While LiveData has one of such observers, it will be considered as active.

If the observer was already added with an owner to this LiveData, LiveData throws an IllegalArgumentException.

Parameters
@NonNull Observer<Object> observer

The observer that will receive the events

removeObserver

@MainThread
public void removeObserver(@NonNull Observer<Object> observer)

Removes the given observer from the observers list.

Parameters
@NonNull Observer<Object> observer

The Observer to receive events.

removeObservers

@MainThread
public void removeObservers(@NonNull LifecycleOwner owner)

Removes all observers that are tied to the given LifecycleOwner.

Parameters
@NonNull LifecycleOwner owner

The LifecycleOwner scope for the observers to be removed.

Extension functions

FlowLiveDataConversions.asFlow

public final @NonNull Flow<@NonNull T> <T extends Object> FlowLiveDataConversions.asFlow(
    @NonNull LiveData<@NonNull T> receiver
)

Creates a Flow containing values dispatched by originating LiveData: at the start a flow collector receives the latest value held by LiveData and then observes LiveData updates.

When a collection of the returned flow starts the originating LiveData becomes active. Similarly, when a collection completes LiveData becomes inactive.

BackPressure: the returned flow is conflated. There is no mechanism to suspend an emission by LiveData due to a slow collector, so collector always gets the most recent value emitted.

LiveDataKt.observe

@MainThread
public final @NonNull Observer<@NonNull T> <T extends Object> LiveDataKt.observe(
    @NonNull LiveData<@NonNull T> receiver,
    @NonNull LifecycleOwner owner,
    @NonNull Function1<@NonNull T, Unit> onChanged
)

Adds the given onChanged lambda as an observer within the lifespan of the given owner and returns a reference to observer. The events are dispatched on the main thread. If LiveData already has data set, it will be delivered to the onChanged.

The observer will only receive events if the owner is in Lifecycle.State.STARTED or Lifecycle.State.RESUMED state (active).

If the owner moves to the Lifecycle.State.DESTROYED state, the observer will automatically be removed.

When data changes while the owner is not active, it will not receive any updates. If it becomes active again, it will receive the last available data automatically.

LiveData keeps a strong reference to the observer and the owner as long as the given LifecycleOwner is not destroyed. When it is destroyed, LiveData removes references to the observer and the owner.

If the given owner is already in Lifecycle.State.DESTROYED state, LiveData ignores the call.

LiveDataReactiveSteamsKt.toPublisher

public final @NonNull Publisher<@NonNull T> <T extends Object> LiveDataReactiveSteamsKt.toPublisher(
    @NonNull LiveData<@NonNull T> receiver,
    @NonNull LifecycleOwner lifecycle
)

Adapts the given LiveData stream to a ReactiveStreams Publisher.

See also
toPublisher

PagingLiveData.cachedIn

public final @NonNull LiveData<@NonNull PagingData<@NonNull T>> <T extends Object> PagingLiveData.cachedIn(
    @NonNull LiveData<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull Lifecycle lifecycle
)

Operator which caches a LiveData of PagingData within the scope of a Lifecycle.

cachedIn multicasts pages loaded and transformed by a PagingData, allowing multiple observers on the same instance of PagingData to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling cachedIn is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData emitted by Pager or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData is an unsupported operation.

Parameters
@NonNull Lifecycle lifecycle

The Lifecycle where the page cache will be kept alive.

PagingLiveData.cachedIn

public final @NonNull LiveData<@NonNull PagingData<@NonNull T>> <T extends Object> PagingLiveData.cachedIn(
    @NonNull LiveData<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull ViewModel viewModel
)

Operator which caches a LiveData of PagingData within the scope of a ViewModel.

cachedIn multicasts pages loaded and transformed by a PagingData, allowing multiple observers on the same instance of PagingData to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling cachedIn is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData emitted by Pager or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData is an unsupported operation.

Parameters
@NonNull ViewModel viewModel

The ViewModel whose viewModelScope will dictate how long the page cache will be kept alive.

PagingLiveData.cachedIn

public final @NonNull LiveData<@NonNull PagingData<@NonNull T>> <T extends Object> PagingLiveData.cachedIn(
    @NonNull LiveData<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull CoroutineScope scope
)

Operator which caches a LiveData of PagingData within a CoroutineScope.

cachedIn multicasts pages loaded and transformed by a PagingData, allowing multiple observers on the same instance of PagingData to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling cachedIn is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData emitted by Pager or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData is an unsupported operation.

Parameters
@NonNull CoroutineScope scope

The CoroutineScope where the page cache will be kept alive. Typically this would be a managed scope such as ViewModel.viewModelScope, which automatically cancels after the PagingData stream is no longer needed. Otherwise, the provided CoroutineScope must be manually cancelled to avoid memory leaks.

TransformationsKt.distinctUntilChanged

public final @NonNull LiveData<@NonNull X> <X extends Object> TransformationsKt.distinctUntilChanged(
    @NonNull LiveData<@NonNull X> receiver
)

Creates a new LiveData object does not emit a value until the source this LiveData value has been changed. The value is considered changed if equals() yields false.

TransformationsKt.map

public final @NonNull LiveData<@NonNull Y> <X extends Object, Y extends Object> TransformationsKt.map(
    @NonNull LiveData<@NonNull X> receiver,
    @NonNull Function1<@NonNull X, @NonNull Y> transform
)

Returns a LiveData mapped from this LiveData by applying transform to each value set on this LiveData.

This method is analogous to io.reactivex.Observable.map.

transform will be executed on the main thread.

Here is an example mapping a simple User struct in a LiveData to a LiveData containing their full name as a String.

val userLD : LiveData<User> = ...;
val userFullNameLD: LiveData<String> = userLD.map { user -> user.firstName + user.lastName }

TransformationsKt.switchMap

public final @NonNull LiveData<@NonNull Y> <X extends Object, Y extends Object> TransformationsKt.switchMap(
    @NonNull LiveData<@NonNull X> receiver,
    @NonNull Function1<@NonNull X, LiveData<@NonNull Y>> transform
)

Returns a LiveData mapped from the input thisLiveData by applying transform to each value set on this.

The returned `LiveData` delegates to the most recent `LiveData` created by [transform] with the most recent value set to `this`, without changing the reference. In this way [transform] can change the 'backing' `LiveData` transparently to any observer registered to the `LiveData` returned by `switchMap()`.

Note that when the backing LiveData is switched, no further values from the older LiveData will be set to the output LiveData. In this way, the method is analogous to io.reactivex.Observable.switchMap.

transform will be executed on the main thread.

Here is an example class that holds a typed-in name of a user String (such as from an EditText) in a MutableLiveData and returns a LiveData containing a List of User objects for users that have that name. It populates that LiveData by requerying a repository-pattern object each time the typed name changes.

This `ViewModel` would permit the observing UI to update "live" as the user ID text changes.

class UserViewModel: AndroidViewModel {
val nameQueryLiveData : MutableLiveData<String> = ...

fun usersWithNameLiveData(): LiveData<List<String>> = nameQueryLiveData.switchMap {
name -> myDataSource.usersWithNameLiveData(name)
}

fun setNameQuery(val name: String) {
this.nameQueryLiveData.value = name;
}
}