‫Welcome back.
‫In this video, we'll learn about Livedata in Android.
‫Livedata is an observable data holder class.
‫Unlike a regular observable Livedata is lifecycle aware, meaning it respects the lifecycle of other
‫app components such as activities, fragments or services.
‫This awareness ensures live data only updates app component observers that are in an active lifecycle
‫state.
‫For example, if the lifecycle status is started or resumed, live data in considers an observer to
‫be in an active state, only active observers are notified of lifecycle updates by livedata.
‫It eliminates memory leaks caused by the multiple callbacks that send the results to the UI thread,
‫ensuring that the UI is always up to date.
‫It decouples tight integration between data mediator and the UI to avoid crashed activities.
‫UI components continuously monitor relevant data and live data manages all these tasks automatically
‫as the relevant lifecycle status changes.
‫If the activity or fragment is recreated due to the configuration changes such as device rotation,
‫portrait to landscape, or vice versa, it immediately receives the latest information from the live
‫data extended live data wraps system services so that they can be shared within the app.
‫Let's jump into Android studio and learn how to use live data in Android applications.
‫Let's continue with our counter application.
‫This is our view model.
‫We have the counter, we have increased counter method and get counter method.
‫And in the main activity we have the layout.
‫I'm using the data binding so you can see data binding build features and the life cycle, which is
‫for the view model.
‫Then I have created the text just for title and this text to display the counter and a button to increase
‫the counter.
‫Now in the main activity I initialized activity main binding and the view model.
‫The problem is here if I remove this main binding the text view whenever I click the button it will
‫close the errors.
‫Let me run the application and show you.
‫If I click on the button, nothing happens because it will increase the counter, but it will not display
‫the counter value.
‫It's not updating the UI, which is the text view with the new counter value.
‫To solve this problem, I'll use the live data in my view model and instead of creating this integer
‫variable called counter, I'll create a similar one with mutable live data of type integer and I'll
‫name it as counter equal to new mutable live data.
‫Live data is an observable data holder class introduced in Android architecture components.
‫It's designed to hold and observe data changes, making it perfect fit for implementing reactive and
‫lifecycle aware data driven UI components.
‫Live data automatically updates the UI, which is the text view in this case, whenever the underlying
‫data changes and ensures that you don't run into issues like memory leaks by being lifecycle aware.
‫Let's continue with our example.
‫I created this mutable live data and here I need to increase the counter.
‫So I'll remove this counter plus plus and I'll change or retrieve this counter variable which is from
‫mutable live data and change it to an int value.
‫So current value I get it from the mutable live data equal to counter dot get value.
‫And if I hover the mouse here unboxing of counter dot get value unboxing because I'm using integer class,
‫not primitive data, so I need to unbox of counter dot get value method may produce null pointer exceptions
‫to avoid null pointer exceptions.
‫I'll use here this syntax, so please follow me and pay attention.
‫If this is not equal to null, then count dot counter dot get value and initialize it to zero.
‫So what I am doing here, this line of code is used to retrieve the current value from the live data
‫object called counter while handling a potential null value gracefully.
‫Counter is a live data object of type integer.
‫Live data is a data holder class that can hold and observe data changes and in this case it holds an
‫integer value count dot get value.
‫Is used to retrieve the current value held by the live data counter.
‫However, the get value method returns a nullable T, which is in this case integer.
‫You can see this t type parameter and in this case it's of type integer, which means it can return
‫null if the data or the live data hasn't been initialized or if the data is null.
‫The code counter dot get value doesn't equal to null.
‫This is a conditional check to determine if the value retrieved from the counter live data is not null.
‫It's a null safety check to ensure that you don't encounter null pointer exception if the live data
‫doesn't have a value yet, Conditional operator is used to handle the two possible cases.
‫If counter dot get value is not null, then the counter dot get value is assigned to the variable current
‫value, which means current value will hold the value stored in the live data.
‫Otherwise, if the count dot counter dot get value equal to null, then zero is assigned to the current
‫value.
‫This provides a default value of zero in case of live data is empty or null.
‫So if it is not null, then assign current value to the counter dot.
‫Get value.
‫Otherwise go and assign it to zero.
‫Then I need to increase the counter by one.
‫So I'll use counter dot set value method current value plus one set.
‫The value method is used to provide the new value you want to assign to the live data in which in this
‫case the current value plus one.
‫So I am updating the live data with a value that is one greater than its current value.
‫Let's go down to the get counter and here I'll change its integer returning type to live data of type
‫integer.
‫So in this case we introduced a live data called counter which will hold an integer value.