From the course: Android Development Essential Training: App Architecture with Kotlin
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Subscribe to changes in LiveData values - Android Tutorial
From the course: Android Development Essential Training: App Architecture with Kotlin
Subscribe to changes in LiveData values
- [Instructor] Once you've created and attached a viewModel to an activity, you can set data in the viewModel and then each time the data changes it'll be broadcast to the activity. The activity can then subscribe to handle the changes to that data whenever it changes. This is called a published subscribe pattern. The viewModel publishes the data, the activity subscribes. I'm going to refactor this app so that instead of declaring the variables here as properties of the activity, they'll become properties of the viewModel. I'll add this code above the init block, and I'll declare two variables. The first one will be named headLine and I'll instantiate them using a constructor of a class called MutableLiveData, the MutableLiveData object has a value and you indicate the type of that value inside the diamond operator, this will be a string and then add the parenthesis at the end, so that you're calling the constructor. Next I'll do the same thing for the dice, and once again I'll use a…
Contents
-
-
-
-
(Locked)
Work with synthetic view bindings5m 10s
-
(Locked)
Get view references with findViewById()4m 53s
-
(Locked)
Handle view events with lambdas5m 26s
-
(Locked)
Create companion object functions6m 16s
-
(Locked)
Update display from new data4m 47s
-
(Locked)
Restore state after orientation change7m 10s
-
(Locked)
Add a ViewModel to an activity4m 43s
-
(Locked)
Subscribe to changes in LiveData values7m 57s
-
(Locked)
-
-
-