LiveData overview Part of Android Jetpack.
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 LiveData only updates app
component observers that are in an active lifecycle state.
LiveData considers an observer, which is represented by the
Observer
class, to be in an
active state if its lifecycle is in the
STARTED
or
RESUMED
state. LiveData only notifies active observers about updates. Inactive
observers registered to watch
LiveData
objects aren't
notified about changes.
You can register an observer paired with an object that implements the
LifecycleOwner
interface. This relationship allows the observer to be removed when the state of
the corresponding
Lifecycle
object changes
to
DESTROYED
.
This is especially useful for activities and fragments because they can safely
observe LiveData
objects
and not worry about leaks—activities and fragments are instantly
unsubscribed when their lifecycles are destroyed.
For more information about how to use LiveData, see Work with LiveData objects.
The advantages of using LiveData
Using LiveData provides the following advantages:
- Ensures your UI matches your data state
- LiveData follows the observer pattern. LiveData notifies
Observer
objects when underlying data changes. You can consolidate your code to update the UI in theseObserver
objects. That way, you don't need to update the UI every time the app data changes because the observer does it for you. - No memory leaks
- Observers are bound to
Lifecycle
objects and clean up after themselves when their associated lifecycle is destroyed. - No crashes due to stopped activities
- If the observer's lifecycle is inactive, such as in the case of an activity in the back stack, then it doesn’t receive any LiveData events.
- No more manual lifecycle handling
- UI components just observe relevant data and don’t stop or resume observation. LiveData automatically manages all of this since it’s aware of the relevant lifecycle status changes while observing.
- Always up to date data
- If a lifecycle becomes inactive, it receives the latest data upon becoming active again. For example, an activity that was in the background receives the latest data right after it returns to the foreground.
- Proper configuration changes
- If an activity or fragment is recreated due to a configuration change, like device rotation, it immediately receives the latest available data.
- Sharing resources
- You can extend a
LiveData
object using the singleton pattern to wrap system services so that they can be shared in your app. TheLiveData
object connects to the system service once, and then any observer that needs the resource can just watch theLiveData
object. For more information, see Extend LiveData.
Work with LiveData objects
Follow these steps to work with
LiveData
objects:
- Create an instance of
LiveData
to hold a certain type of data. This is usually done within yourViewModel
class. - Create an
Observer
object that defines theonChanged()
method, which controls what happens when theLiveData
object's held data changes. You usually create anObserver
object in a UI controller, such as an activity or fragment. Attach the
Observer
object to theLiveData
object using theobserve()
method. Theobserve()
method takes aLifecycleOwner
object. This subscribes theObserver
object to theLiveData
object so that it is notified of changes. You usually attach theObserver
object in a UI controller, such as an activity or fragment.
When you update the value stored in the LiveData
object, it triggers all
registered observers as long as the attached LifecycleOwner
is in the active
state.
LiveData allows UI controller observers to subscribe to updates. When the data
held by the LiveData
object changes, the UI automatically updates in response.
Create LiveData objects
LiveData is a wrapper that can be used with any data, including objects that
implement Collections
, such as List
. A
LiveData
object is usually
stored within a ViewModel
object and is accessed via a getter method, as demonstrated in the following
example:
Kotlin
class NameViewModel : ViewModel() { // Create a LiveData with a String val currentName: