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

Subscribe to changes in LiveData values - Android Tutorial

From the course: Android Development Essential Training: App Architecture with Kotlin

Start my 1-month free trial

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