MutableLiveData
open class MutableLiveData<T : Any!> : LiveData<T>
LiveData
which publicly exposes setValue(T)
and postValue(T)
method.
Summary
Public constructors
|
Creates a MutableLiveData initialized with the given value .
|
Creates a MutableLiveData with no value assigned to it.
|
Inherited functions
|
From class LiveData
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.
|
Boolean |
hasActiveObservers()
Returns true if this LiveData has active observers.
|
Boolean |
hasObservers()
Returns true if this LiveData has observers.
|
Unit |
observe(@NonNull owner: LifecycleOwner, @NonNull observer: Observer<in T>)
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 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 & the owner.
If the given owner is already in Lifecycle.State#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 .
|
Unit |
observeForever(@NonNull observer: Observer<in T>)
Adds the given observer to the observers list. This call is similar to LiveData#observe(LifecycleOwner, Observer) 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(Observer) 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 .
|
Unit |
onActive()
Called when the number of active observers change to 1 from 0.
This callback can be used to know that this LiveData is being used thus should be kept up to date.
|
Unit |
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 Lifecycle.State#STARTED or Lifecycle.State#RESUMED (like an Activity in the back stack).
You can check if there are observers via hasObservers() .
|
Unit |
removeObserver(@NonNull observer: Observer<in T>)
Removes the given observer from the observers list.
|
Unit |
removeObservers(@NonNull owner: LifecycleOwner)
Removes all observers that are tied to the given LifecycleOwner .
|
|
Public constructors
<init>
MutableLiveData(value: T)
Creates a MutableLiveData initialized with the given value
.
Parameters |
value |
T: initial value |
<init>
MutableLiveData()
Creates a MutableLiveData with no value assigned to it.
Public methods
postValue
open fun postValue(value: T): Unit
setValue
open fun setValue(value: T): Unit