When to use Android’s LiveData and Observable field?

Both have their use-cases, for instance:

  • If you want a life-cycle tolerant container for your UI state model, LiveData is the answer.

  • If you want to make the UI update itself when a piece of logic is changed in your view model, then use ObservableFields.

I myself prefer using a combination of LivaData and ObservableField/BaseObservable, the LiveData will normally behave as a life-cycle aware data container and also a channel between the VM and the View.

On the other hand the UI state model objects that are emitted through the LiveData are themselves BaseObservable or have their fields as ObservableField.

That way I can use the LiveData for total changes of the UI state. And set values to the UI state model ObservableField fields whenever a small portion of the UI is to be updated.

Edit: Here is a quick illustration on a UserProfile component for example:

UIStateModel

data class ProfileUIModel(
    private val _name: String,
    private val _age: Int
): BaseObservable() {
    var name: String
        @Bindable get() = _name
        set(value) {
          _name = value
          notifyPropertyChanged(BR.name)
        }
    var age: Int
        @Bindable get() = _age
        set(value) {
          _age = value
          notifyPropertyChanged(BR.age)
        }
}

ViewModel

class UserProfileViewModel: ViewModel() {

    val profileLiveData: MutableLiveData = MutableLiveData()

    ...

    // When you need to rebind the whole profile UI object.
    profileLiveData.setValue(profileUIModel)

    ...

    // When you need to update a specific part of the UI.
    // This will trigger the notifyPropertyChanged method on the bindable field "age" and hence notify the UI elements that are observing it to update.
    profileLiveData.getValue().age = 20 
}

View

You'll observe the profile LiveData changes normally.

XML

You'll use databinding to bind the UI state model.

Edit: Now the mature me prefers Immutability instead of having mutable properties as explained in the answer.