Use of ViewModel with LiveData in Android
In Android Architecture Components, ViewModel and LiveData are used to change values and notify observables with respect to android lifecycle.
The
ViewModelclass is designed to store and manage UI-related data in a lifecycle conscious way. TheViewModelclass allows data to survive configuration changes such as screen rotations.
LiveDatais 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 LiveData only updates app component observers that are in an active lifecycle state.