‫Hello developers, and welcome back.
‫In this video we're going to 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
‫application components such as activities, fragments or services.
‫This awareness ensures LiveData only updates app components observers that are in an active lifecycle
‫state.
‫If the lifecycle state is started or resumed, live data 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 multiple callbacks that send 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 LiveData manages all these tasks automatically
‫as the relevant lifecycle status changes.
‫If the activity or fragment is recreated due to configuration changes such as a device, rotation,
‫portrait or landscape, it immediately receives the latest information from LiveData.
‫Extended LiveData wraps system services so that they can be shared within the app.
‫Let's move to Android Studio and add LiveData to our application.
‫So let's enhance the previous example by incorporating LiveData to make the data observable and update
‫the UI automatically when the data changes.
‫At first I need to add the LiveData dependency again.
‫You can go to the same website Developer.android.com slash jetpack slash Android slash releases slash
‫lifecycle.
‫Also scroll down.
‫You can choose for the Kotlin and select Kotlin.
‫Scroll down and copy this LiveData back to our Android Studio.
‫And here we have in the Build.gradle file the ViewModel.
‫I'll paste the LiveData dependency with the same lifecycle version sync.
‫Now.
‫And as mentioned before, the LiveData is a part of the Android architecture components and is used
‫for building data objects that notify views when the underlying database changes.
‫Then we need to add it to the ViewModel here.
‫Open the ViewModel class counter ViewModel and I'll change the type of this variable from an int to
‫a mutable live data equals to mutable live live data.
‫It's of type int, and I'll initialize it like this.
‫Here I'm creating an instance of mutable live data class in Kotlin with type parameter of integer.
‫Mutable live data is a class provided by Android Architecture components.
‫It's a subclass of live data that holds and manages observable data.
‫The type parameter int specifies the type of data that the mutable live data will hold.
‫In this case, it's an integer.
‫So here I'm creating a counter variable of type immutable live data that holds integers.
‫This variable can be updated with new values over time, and any observers that are registered to it
‫will be notified when the value changes.
‫In the context of this example, it's being used to hold and manage the counter value in the counter
‫view model.
‫Then I'll use the init block init block to set the value for this counter to zero.
‫Here I'm initializing the value counter to zero because it's a mutable live data.
‫I can't set like counter equals to zero.
‫I need to use counter dot value because it's a mutable live data.
‫Then I need to increase the counter.
‫You can see there is an error here.
‫I need to replace it with counter dot value.
‫I need to increase it by one.
‫I can't write like this because it's a mutable live data, so I need to use counter value.
‫I'm getting it and if it is not null, go and use this method called plus.
‫Here I'm adding number one or every time.
‫Time this method is called.
‫The value will be increased by one and restored inside the counter dot value.
‫So again guys counter dot value.
‫This retrieves the current value held by the mutable live data instance named counter.
‫The value property is used to get or set the current value of the mutable live data, and in this case
‫I'm setting it equals counter dot value.
‫In this case I am getting the value and this is the safe operator.
‫Safe call operator.
‫It's used to invoke the plus function on the current value of the counter, and one is added to it.
‫The safe call operator is used to prevent a null pointer exception in case of counter value is currently
‫null.
‫If counter dot value is null, the entire expression evaluates to null.
‫So in this case I'm increasing the counter value by one.
‫So this is how we create the live data inside the ViewModel.
‫Now let's move to the main activity.
‫I'll remove all of these codes because I don't need them anymore.
‫And now let's move to the activity underscore main.
‫Please pay attention here I'll create a data variable here and I'll name it as my view model.
‫The type of it will be com dot Mr. coding dot viewmodel dot counter view model.
‫In the next video we're going to use this variable to access the text and to get the current counter
‫value to execute the function, increase counter that it is inside the view model.