From the course: Android Development Essential Training: 3 Navigation

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: 3 Navigation

Start my 1-month free trial

Subscribe to changes in LiveData values

- [Instructor] Historically, Android applications may have pulled the data store or blindly requested the latest data in response to lifecycle changes. However, that's a thing of the past with the use of LiveData. LiveData allows UI components to observe data changes automatically. In our MainViewModel class, we have already created a mutable LiveData object here on line number 11. However, if you recall, we've intentionally made this object private. So only our ViewModel can update it. If we want to expose the underlying data to our activity, we'll need to create a new variable. This time it's going to be of type LiveData. That means that we won't be exposing any public methods to modify the contained value. So right underneath line number 11, we're going to create a new variable. This time we're going to call it info and it's going to be of type LiveData. It's also going to store an integer. And let's use one of…

Contents