Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to combine android ViewModel and data binding

I am a little confused about how to combine 2 techniques in android, namely

  • ViewModel (https://developer.android.com/topic/libraries/architecture/viewmodel) and
  • Data Binding Library (https://developer.android.com/topic/libraries/data-binding)

ViewModel should handle business logic, the layer behind the actual view and send data to the view with something like LiveData. The view observes to this LiveData and updates itself on changes

Data Binding Library exists to make it easier to bind to the view and interact with the view on another level (for example by updating some properties of some class)

The questions:

  • Should the properties / model property of Data Binding Library be kept inside of ViewModel class (A) or in the view (activity, fragment) (B)
  • If (A) : If the Data Binding Library properties / models are kept in ViewModel class, is it considered bad practice that view logic is executed inside ViewModel by changing data from the data binding library?
  • Is there a good code example (some GitHub repo) where there is an example of a decent combination of those 2 concepts?

Update: Found official documentation for my issue. Here is the link: https://developer.android.com/topic/libraries/data-binding/architecture#viewmodel

like image 795
ramden Avatar asked May 30 '26 15:05

ramden


1 Answers

How data binding works

Consider using LiveData, it lives inside the ViewModel and is how the data binding library knows that you must update for example the string of a TextView.

What data binding actually does is something similar to what you would explicitly do in your fragment:

Subscribe from your Kotlin code (Fragment/Activity) to a LiveData property that lives within the ViewModel but in this case, data binding will update the view values for you since you will indicate it before from your XML Layout.

So the answer is (A):

You could have a ViewModel class with properties of type LiveData<T> and from your Layout, you can use them directly without subscribing explicitly from your kotlin code as I mentioned before, which continues to guarantee that the ViewModel continues being the provider of information for the user's view, the difference is that instead of you are doing it explicitly, data binding will do it for you.

class MyViewModel : ViewModel {
    // view model doesn't know if Fragment/Activity is using data binding or not, it just continues providing info as normal.
    val myString : MutableLiveData<String> = MutableLiveData()

    init {
        myString.value = "a value that data binding will print in a TextView for you"
    }

    private fun changeMyString() {
        // Change the value in the future when you want and then data binding will print the text in your TextView for you.
        myString.value = "other value to that TextView"
    }
}

Layout:

<TextView
    android:text="@{myViewModel.myString}" />

Resources

This Google Codelab is pretty useful, it helped me when I started with data binding because it is prepared to teach.

If you just want to go directly to code, android/sunflower is a repository that uses data binding and in general provides useful samples of jetpack features.

like image 78
Julio Lemus Avatar answered Jun 02 '26 05:06

Julio Lemus