Bind layout views to Architecture Components

The AndroidX library includes the Architecture
Components, which you can
use to design robust, testable, and maintainable apps.
The Data Binding Library works seamlessly with the Architecture
Components to further simplify the
development of your UI. The layouts in your app
can bind to the data in the Architecture Components, which already help you
manage the UI controllers lifecycle and notify about changes in the data.

This page shows how to incorporate the Architecture Components to your app to
further enhance the benefits of using the Data Binding Library.

Use LiveData to notify the UI about data changes

You can use LiveData objects as
the data binding source to automatically notify the UI about changes in the
data. For more information about this Architecture Component, see LiveData
Overview.

To use a LiveData object with your binding class, you need to specify a
lifecycle owner to define the scope of the LiveData object. The following
example specifies the activity as the lifecycle owner after the binding class
has been instantiated:

You can use a ViewModel
component, as explained in Use ViewModel to manage UI-related
data, to bind the data to the layout. In the ViewModel component,
you can use the LiveData object to transform the data or merge multiple data
sources. The following example shows how to transform the data in the ViewModel:

Java

Use ViewModel to manage UI-related data

The Data Binding Library works seamlessly with
ViewModel components, which
expose the data that the layout observes and reacts to its changes. Using
ViewModel components with the Data Binding Library allows you to move UI logic
out of the layouts and into the components, which are easier to test. The Data
Binding Library ensures that the views are bound and unbound from the data
source when needed. Most of the remaining work consists in making sure that
you're exposing the correct data. For more information about this Architecture
Component, see ViewModel
Overview.

To use the ViewModel component with the Data Binding Library, you must
instantiate your component, which inherits from the
ViewModel class, obtain an
instance of your binding class, and assign your ViewModel component to a
property in the binding class. The following example shows how to use the
component with the library:

Use an Observable ViewModel for more control over binding adapters

You can use a ViewModel
component that implements the
Observable to notify other
app components about changes in the data, similar to how you would use a
LiveData object.

There are situations where you might prefer to use a
ViewModel component that
implements the Observable
interface over using LiveData objects, even if you lose the lifecycle
management capabilities of LiveData. Using a ViewModel component that
implements Observable gives you more control over the binding adapters in your
app. For example, this pattern gives you more control over the notifications
when data changes, it also allows you to specify a custom method to set the
value of an attribute in two-way data binding.