From the course: Android Development: Data Binding

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Binding to ViewModels

Binding to ViewModels - Android Tutorial

From the course: Android Development: Data Binding

Start my 1-month free trial

Binding to ViewModels

- [Instructor] We've seen how binding to observable data sources can solve the problem of getting changes to the data to propagate to the layout. But in the previous example, we used observable fields within our data class. In this example, we're going to use the ViewModel, one of the Android architecture components as the data model instead of a custom class that uses observable fields. This has some advantages over the observable class model. Most importantly that ViewModels understand the activity lifecycle. If you're not already familiar with ViewModels, then you might want to review the architecture components documentation and specifically the docs for the ViewModel class at this link. All right, so let's go to Android Studio and let's build the code for this example. So first, let's open the code for the activity and that is going to be here in the ViewModExample. This code is not really that much different…

Contents