- Android Data Binding Library — From Observable Fields to LiveData in two steps
- Jose Alcerreca
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: Rickon
Android Data Binding Library – Just two steps from observable domain to LiveData
One of the most important features of data binding is observability. You can use it to bind data to UI elements so that when the data changes, the related elements are updated on the screen.
By default, ordinary primitives and strings are not observable, so if they are used in the data binding layout, their values will be used when the binding is created, but subsequent changes to them will be ignored.
To make objects observable, our data binding library includes a series of observable classes: ObservableBoolean, ObservableInt, ObservableDouble, and the stereotype: ObservableField
. From now on, we’ll call these observable domains.
A few years later, as part of the first wave of architectural components, we released LiveData, another observable. This is a candidate for data binding compatibility, so we added this feature.
LiveData is lifecycle aware, which is not a great advantage for the observable domain because the data binding library already checks when the view is active. However, LiveData supports Transformations and many architectural components, such as Room and WorkManager.
** For these reasons, it is recommended that you migrate to LiveData. ** You only need two steps to complete.
Step 1: Use LiveData instead of the observable domain
If you use observables directly in the data binding layout, simply replace ObservableSomething (or ObservableField
) with LiveData
.
Modify before:
<data>
<import type="android.databinding.ObservableField"/>
<variable
name="name"
type="ObservableField< String>" />
</data>
…
<TextView
android:text="@{name}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Copy the code
Remember that
%lt;
is not a typo. You have to escape the<
character inside XML layouts.
Revised:
<data>
<import type="android.arch.lifecycle.LiveData" />
<variable
name="name"
type="LiveData< String>" />
</data>
…
<TextView
android:text="@{name}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Copy the code
Alternatively, if you expose the observable from the ViewModel (preferred method) or a Presenter layer or controller, no layout changes are required. Just replace those ObservableFields with LiveData in the ViewModel.
Modify before:
class MyViewModel : ViewModel() {
val name = ObservableField<String>("Ada")}Copy the code
Revised:
class MyViewModel : ViewModel() {
private val _name = MutableLiveData<String>().apply { value = "Ada" }
val name: LiveData<String> = _name // Expose the immutable version of the LiveData
}
Copy the code
Step 2: Set the life cycle owner of LiveData
The binding class has a method called setLifecycleOwner that must be called when viewing LiveData from the data binding layout.
Modify before:
val binding = DataBindingUtil.setContentView<TheGeneratedBinding>(
this,
R.layout.activity_data_binding
)
binding.name = myLiveData // or myViewModel
Copy the code
Revised:
val binding = DataBindingUtil.setContentView<TheGeneratedBinding>(
this,
R.layout.activity_data_binding
)
binding.lifecycleOwner = this // Use viewLifecycleOwner for fragments
binding.name = myLiveData // or myViewModel
Copy the code
Note: if you want to set the content of the fragments, it is recommended to use fragments. ViewLifecycleOwner (rather than fragments of life cycle) to deal with potential separation fragments could.
Now you can use your LiveData objects with Transformations and MediatorLiveData. If you’re not familiar with these features, check out the video “Fun with LiveData” from Android Developer Conference 2018.
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.