Class MutableLiveData<T>

java.lang.Object
icyllis.modernui.lifecycle.LiveData<T>
icyllis.modernui.lifecycle.MutableLiveData<T>
Type Parameters:
T - The type of data hold by this instance
Direct Known Subclasses:
MediatorLiveData

public class MutableLiveData<T> extends LiveData<T>
LiveData which publicly exposes setValue(T) and postValue(T) method.
  • Constructor Details

    • MutableLiveData

      public MutableLiveData(T value)
      Creates a MutableLiveData initialized with the given value.
      Parameters:
      value - initial value
    • MutableLiveData

      public MutableLiveData()
      Creates a MutableLiveData with no value assigned to it.
  • Method Details

    • postValue

      public void postValue(T value)
      Description copied from class: LiveData
      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");
       
      The value "b" would be set at first and later the main thread would override it with the value "a".

      If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.

      Overrides:
      postValue in class LiveData<T>
      Parameters:
      value - The new value
    • setValue

      public void setValue(T value)
      Description copied from class: LiveData
      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 LiveData.postValue(Object)

      Overrides:
      setValue in class LiveData<T>
      Parameters:
      value - The new value