LiveData
public abstract class LiveData<T>
MutableLiveData |
|
SliceLiveData.CachedSliceLiveData |
Implementation of |
MediatorLiveData |
|
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 |
|---|
LiveData()Creates a LiveData with no value assigned to it. |
LiveData(T value)Creates a LiveData initialized with the given |
Public methods |
|
|---|---|
@Nullable T |
getValue()Returns the current value. |
boolean |
Returns true if this LiveData has active observers. |
boolean |
Returns true if this LiveData has observers. |
boolean |
Returns whether an explicit value has been set on this LiveData. |
void |
@MainThreadAdds the given observer to the observers list within the lifespan of the given owner. |
void |
@MainThreadAdds the given observer to the observers list. |
void |
@MainThreadRemoves the given observer from the observers list. |
void |
Removes all observers that are tied to the given |
Protected methods |
|
|---|---|
void |
onActive()Called when the number of active observers change from 0 to 1. |
void |
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 |
@MainThreadSets the value. |
Extension functions |
|
|---|---|
final @NonNull Flow<@NonNull T> |
Creates a |
final @NonNull Observer<@NonNull T> |
@MainThreadThis method is deprecated. This extension method is not required when using Kotlin 1.4. |
final @NonNull Publisher<@NonNull T> |
<T extends Object> LiveDataReactiveStreams.toPublisher(Adapts the given |
final @NonNull LiveData<@NonNull PagingData<@NonNull T>> |
<T extends Object> PagingLiveData.cachedIn(Operator which caches a |
final @NonNull LiveData<@NonNull PagingData<@NonNull T>> |
<T extends Object> PagingLiveData.cachedIn(Operator which caches a |
final @NonNull LiveData<@NonNull PagingData<@NonNull T>> |
<T extends Object> PagingLiveData.cachedIn(Operator which caches a |
final @NonNull LiveData<@NonNull X> |
@MainThreadCreates a new |
final @NonNull LiveData<@NonNull Y> |
@MainThreadReturns a |
final @NonNull LiveData<@NonNull Y> |
@MainThreadReturns a |
Public constructors
Public methods
getValue
public @Nullable 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 | |
|---|---|
@Nullable T |
the current value or null if |
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 |
isInitialized
public boolean isInitialized()
Returns whether an explicit value has been set on this LiveData. If this returns true, then the current value can be retrieved from getValue.
Note that calling this method on a background thread may still result in this method returning false even if a call to postValue is being processed.
| Returns | |
|---|---|
boolean |
whether an explicit value has been set on this LiveData |
observe
@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> 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<T> observer |
The observer that will receive the events |
observeForever
@MainThread
public void observeForever(@NonNull Observer<T> 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.
removeObserver
@MainThread
public void removeObserver(@NonNull Observer<T> observer)
Removes the given observer from the observers list.
removeObservers
@MainThread
public void removeObservers(@NonNull LifecycleOwner owner)
Removes all observers that are tied to the given LifecycleOwner.
| Parameters | |
|---|---|
@NonNull LifecycleOwner owner |
The |
Protected methods
onActive
protected void onActive()
Called when the number of active observers change from 0 to 1.
This callback can be used to know that this LiveData is being used thus should be kept up to date.
onInactive
protected void onInactive()
Called when the number of active observers change from 1 to 0.
This does not mean that there are no observers left, there may still be observers but their lifecycle states aren't STARTED or RESUMED (like an Activity in the back stack).
You can check if there are observers via hasObservers.
postValue
protected void postValue(T value)
Posts a task to a main thread to set the given value. So if you have a following code executed in the main thread:
liveData.postValue("a");
liveData.setValue("b");If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.
| Parameters | |
|---|---|
T value |
The new value |
setValue
@MainThread
protected void setValue(T value)
Sets the value. If there are active observers, the value will be dispatched to them.
This method must be called from the main thread. If you need set a value from a background thread, you can use postValue
| Parameters | |
|---|---|
T value |
The new value |
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.
LiveDataReactiveStreams.toPublisher
public final @NonNull Publisher<@NonNull T> <T extends Object> LiveDataReactiveStreams.toPublisher(
@NonNull LiveData<@NonNull T> receiver,
@NonNull LifecycleOwner lifecycle
)
Adapts the given LiveData stream to a ReactiveStreams Publisher.
By using a good publisher implementation such as RxJava 2.x Flowables, most consumers will be able to let the library deal with backpressure using operators and not need to worry about ever manually calling Subscription.request.
On subscription to the publisher, the observer will attach to the given LiveData. Once Subscription.request is called on the subscription object, an observer will be connected to the data stream. Calling request(Long.MAX_VALUE) is equivalent to creating an unbounded stream with no backpressure. If request with a finite count reaches 0, the observer will buffer the latest item and emit it to the subscriber when data is again requested. Any other items emitted during the time there was no backpressure requested will be dropped.
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.
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 |
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 |
Transformations.distinctUntilChanged
@MainThread
public final @NonNull LiveData<@NonNull X> <X extends Object> Transformations.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.
Transformations.map
@MainThread
public final @NonNull LiveData<@NonNull Y> <X extends Object, Y extends Object> Transformations.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 }
Transformations.switchMap
@MainThread
public final @NonNull LiveData<@NonNull Y> <X extends Object, Y extends Object> Transformations.switchMap(
@NonNull LiveData<@NonNull X> receiver,
@NonNull Function1<@NonNull X, LiveData<@NonNull Y>> transform
)
Returns a LiveData mapped from the input this LiveData 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;
}
}