Skip to content
tezvyn:

LiveData: Lifecycle-Aware Data Observation

Source: developer.android.comMediumHow cards are made

LiveData: Lifecycle-Aware Data Observation

LiveData is a data holder that only notifies active UI components. It connects your ViewModel's data to your UI, preventing crashes and memory leaks by respecting the Android lifecycle.

Why it exists

Before LiveData, developers manually managed the connection between data sources and the UI. This meant writing boilerplate code in onStart() and onStop() to subscribe and unsubscribe, a process that was tedious and a common source of memory leaks and crashes when updating a UI that was no longer visible.

The mental model

Think of LiveData as a smart bulletin board for your app's data. UI components, like an Activity, can "watch" this board. LiveData is smart enough to know if the watcher is actually looking (is the UI active?). It only posts updates for active watchers and automatically removes the subscription when the watcher leaves for good (is destroyed). When a watcher returns, like after a screen rotation, it immediately sees the latest notice on the board.

How it works

LiveData uses the observer pattern and is aware of Android's Lifecycle. You hold data in a LiveData object, usually in a ViewModel. In your UI (an Activity or Fragment, which are LifecycleOwners), you create an Observer and attach it to the LiveData. LiveData checks the owner's state and only delivers updates by calling the observer's onChanged() method if the lifecycle is STARTED or RESUMED. When the owner is DESTROYED, LiveData automatically cleans up the observer, preventing leaks.

When to use it

Use LiveData to bridge the gap between your ViewModel and your UI. It's perfect for displaying data that changes over time, like user profiles or sensor readings. It shines when handling configuration changes (like device rotation) because it automatically provides the latest data to the newly created UI component, ensuring consistency without extra code.

When not to use it

LiveData is tied to the Android framework. For business logic in your data or domain layers that should be platform-agnostic, Kotlin Flows are a better choice. Also, because LiveData caches and re-emits its last value to new observers, it's not ideal for one-shot events (like showing a Toast), which could be triggered multiple times unintentionally.

One canonical example

A ViewModel fetches a user's profile and stores it in a MutableLiveData<User>. An Activity observes this LiveData. When the profile data arrives, the ViewModel updates the LiveData's value. The Observer in the Activity receives the new User object and updates the TextViews on screen. If the user navigates away and comes back, the UI is automatically updated with the latest data held by LiveData.

Interview question

When would using LiveData be an inappropriate choice for handling data updates?

  • a.When bridging data from a ViewModel to an Activity or Fragment.
  • b.When displaying data that changes frequently, like sensor readings.
  • c.When needing to update UI components after a configuration change, such as screen rotation.
  • d.When implementing platform-agnostic business logic in data or domain layers.Correct
Why?

The card explicitly states that LiveData is tied to the Android framework and is not ideal for platform-agnostic business logic in data or domain layers, suggesting Kotlin Flows instead. The other options describe scenarios where LiveData is recommended or performs effectively.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles