Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LiveData with Data Binding

With the stabilization of Android Architecture Components I started updating all my basic ViewModels to the new implementation ofViewModel. In my understanding, the usage of LiveData is recommended to hold the Model class since it handles the lifecycle better.

I like using Data Binding because it makes the code clearer in Java/Kotlin side and it is not needed to "watch" the value changes to update the UI. However the layout using Data Binding only watch data changes if the Model (or the ViewModel) extends BaseObservable and LiveData does not. I understand the one of the main objectives of LiveData is to be observed and updates the UI programmatically but for simple updates, Data Binding is very useful.

This issue was already reported (GitHub and Stack Overflow) and first was said that the version 1.0 would have it and now is said that this feature is in development.

In order to use both LiveData and Data Binding, I created a very simple implementation of class that extends BaseObservable:

import android.arch.lifecycle.LiveData import android.arch.lifecycle.MutableLiveData import android.databinding.BaseObservable  class ObservableMutableLiveData<T>() : BaseObservable() {      private var data: MutableLiveData<T> = MutableLiveData()      constructor(data: T) : this() {         this.data.value = data     }      public fun set(value: T) {         if (value != data.value) {             data.value = value             notifyChange()         }     }      public fun get(): T? {         return data.value     }      public fun getObservable(): LiveData<T> {         return data     } } 

So basically my ObservableMutableLiveData is a copy of ObservableField using LiveData to store the model and with this implementation, the layout updates after every model update.

The questions are:

  • Is this a bad implementation of LiveData? Does this wrapper "breaks" the functionalities of LiveData, such as be lifecycle-aware?
  • In my understanding, LiveData is the new ObservableField. Is this correct?
like image 966
Igor Escodro Avatar asked Dec 09 '17 09:12

Igor Escodro


People also ask

What is the advantage of implementing LiveData and data binding?

The advantages of using LiveData Using LiveData provides the following advantages: Ensures your UI matches your data state. LiveData follows the observer pattern. LiveData notifies Observer objects when underlying data changes.

Can I use both DataBinding and ViewBinding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

What is data binding in MVVM Android?

Up until now, we've used Data Binding to update the View from the ViewModel. LiveData is a handy data holder that acts as a container over the data to be passed. The best thing about LiveData is that it is lifecycle aware. So if you are in the background, the UI won't try to update.

How LiveData is different from ObservableField?

ObservableField is not Lifecycle aware but LiveData is lifecycle aware and hence will notify only the observables which are “active”.

Is it better to use livedata or data binding for model?

In my understanding, the usage of LiveData is recommended to hold the Model class since it handles the lifecycle better. I like using Data Binding because it makes the code clearer in Java/Kotlin side and it is not needed to "watch" the value changes to update the UI.

Does livedata always need a lifecycle?

LiveData always need a lifecycle. But, in this code LiveData directly work with data binding. So, we should set the activity (this activity) as the lifecycle owner to the data binding instance. Here is the final MainActivity.kt code.

What is livedata in Android?

To understand that, you should be good with the basics. In simple terms, LiveData is an observable data holder class. This means it can hold a set of data that can be observed from other Android components like Activities, Fragments, and Services. It’s lifecycle-aware and mostly used to update UI from the ViewModel in MVVM architecture projects.

Can livedata be used as observable field in Android Studio?

EDIT : Use androidx.lifecycle.MutableLiveData<String> as type if you use androidx. Show activity on this post. The Android Studio 3.1 (currently in Canary 6) will fix this issue, since LiveData can be used as observable field: You can now use a LiveData object as an observable field in data binding expressions.


2 Answers

For those who came across this question looking for an example like I did, here's one:

In layout .xml put the LiveData element with it's type:

<layout>     <data>         <variable             name="myString"             type="android.arch.lifecycle.MutableLiveData&lt;String&gt;"/>     </data>      ...      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text='@{myString}'         ...      />      ...  </layout> 

In your code set it's value and the lifecycle owner:

MutableLiveData<String> myString = new MutableLiveData<>();  ...  binding.setLifecycleOwner(this); binding.setMyString(myString); 

That's it :)

Note that the default value of LiveData elements is null so assign initial values to make sure you get the desired effect right away or use this to enforce nullability.

EDIT: Use androidx.lifecycle.MutableLiveData&lt;String&gt; as type if you use androidx.

like image 167
Sir Codesalot Avatar answered Oct 05 '22 23:10

Sir Codesalot


The Android Studio 3.1 (currently in Canary 6) will fix this issue, since LiveData can be used as observable field:

Updates to Data Binding:

You can now use a LiveData object as an observable field in data binding expressions. The ViewDataBinding class now includes a new setLifecycle method that you need to use to use to observe LiveData objects.

Source: Android Studio 3.1 Canary 6 is now available

like image 34
Igor Escodro Avatar answered Oct 06 '22 00:10

Igor Escodro