‫We learned about live data in Android.
‫Now let's observe the live data to update the UI.
‫Whenever the data changes, I need to update the UI component, which is a text view to here.
‫I'll start observing the live data view model dot get counter and you can see get counter returns.
‫Live data of type integer dot observe method.
‫And here I need to pass two parameters this and the new observer of type integer.
‫Let's talk about this line of code and the observe method.
‫ViewModel dot get counter dot observe method is used to observe the live data counter when the counter
‫data changes.
‫For example, when you call increment count and increase it by one in the view model, the on changed
‫method is triggered allowing you to update the UI accordingly.
‫This way, live data ensures that your UI always displays the latest data without any manual update
‫calls.
‫Live data is also lifecycle aware, which means it will automatically start and stop observing data
‫based on the lifecycle state of the activity or fragment preventing potential memory leaks.
‫In summary, live data, when used with ViewModel, provides a clean and reactive way to manage and
‫observe data changes in your Android app while maintaining proper lifecycle management.
‫So in this on changed method, I need to update the UI when the live data changes, so main binding
‫dot text view number two dot set text to counter value or this integer.
‫So I'll change this parameter counter and here I'll set it directly to this counter.
‫I'll remove this and let's go up and the onClick method, no need to update the text view so you can
‫see this is the minimal code that we can use in the Oncreate method.
‫Let's run.
‫And here we go.
‫This is our application.
‫Click on increase counter.
‫You can see the counter is updated.
‫Then if we rotate the screen, you can see that the ViewModel and the live data will preserve the data.
‫And this is the value.
‫Six If I increase the counter to 12, then rotate the screen the live data and the ViewModel will keep
‫the counter value.
‫This way, live data ensures that your UI always displays the latest data without any manual update
‫calls.
‫Live data is also lifecycle aware, which means it will automatically start and stop observing data
‫based on the lifecycle state of that activity or fragment preventing potential memory leaks and just
‫for mentioning this important note that since I'm using the data binding, I can get rid of this line.
‫So I'll go to the ViewModel here and I'll change the method view, view in order to call this method
‫in the layout, I start by creating a data variable.
‫It's similar to what we've created before my view model com master coding and my view model.
‫And here in the button I'll call onClick method and I'll add the syntax of the data binding my ViewModel
‫I'll call increase counter.
‫Then here I can remove this and I need to link the main binding with the ViewModel main binding dot.
‫Set my view model to the view model.
‫I am just linking the ViewModel with the data binding.
‫So in this example we reviewed the main binding or the data binding the view model and we learned about
‫the live data.
‫And this is very good example and I want from you to code it by yourself, okay?
‫So you can see increase the counter, rotate the screen and you can see the counter is increased again
‫starting from eight.