Questions tagged [android-livedata]

Android LiveData holds the value and allow it to observe changes and also respects lifecycle of the app components.

LiveData is part of new Android Architecture Components

LiveData used as holder to put values and allow it to be observed. It also respects lifecycle of the app components viz. onCreate(), onDestroy() etc. It is part of Android Architecture Lifecycle library which comes with Lifecycles, LiveData, and ViewModel.

LiveData is usually used with ViewModel to bind it with view and observe the changes accordingly. It is active when one or more active Observers are subscribed and is in-active when they are no active observers.(Active Observers means observers [Activity, Fragment] which are in STARTED or RESUMED state. As it is Lifecycle aware we can share it among multiple activities and fragments etc.

Diagram explains its place in Android Architecture Components. Android Architecture Component Flow

As shown in diagram, It gets data from repository and can be observed in Activity or Fragment via ViewModel

You can find more about it below

2445 questions
133
votes
7 answers

Difference of setValue() & postValue() in MutableLiveData

There are two ways that make change value of MutableLiveData. But what is difference between setValue() & postValue() in MutableLiveData. I could not find documentation for same. Here is class MutableLiveData of Android. package…
Khemraj Sharma
  • 46,529
  • 18
  • 168
  • 182
104
votes
7 answers

Observing LiveData from ViewModel

I have a separate class in which I handle data fetching (specifically Firebase) and I usually return LiveData objects from it and update them asynchronously. Now I want to have the returned data stored in a ViewModel, but the problem is that in…
100
votes
3 answers

Why there's a separate MutableLiveData subclass of LiveData?

It looks like MutableLiveData differs from LiveData only by making the setValue() and postValue() methods public, whereas in LiveData they are protected. What are some reasons to make a separate class for this change and not simply define those…
94
votes
6 answers

MutableLiveData: Cannot invoke setValue on a background thread from Coroutine

I'm trying to trigger an update on LiveData from a coroutine: object AddressList: MutableLiveData>() fun getAddressesLiveData(): LiveData> { AddressList.value = listOf() GlobalScope.launch { …
kike
  • 3,157
  • 3
  • 21
  • 39
91
votes
9 answers

Notify Observer when item is added to List of LiveData

I need to get an Observer event when the item is added to the List of LiveData. But as far as I understand the event receives only when I replace the old list with a new one. For example when I do the next: list.value =…
Ruslan Leshchenko
  • 2,865
  • 2
  • 16
  • 31
85
votes
13 answers

LiveData remove Observer after first callback

How do I remove the observer after I receive the first result? Below are two code ways I've tried, but they both keep receiving updates even though I have removed the observer. Observer observer = new Observer() { @Override …
galaxigirl
  • 1,770
  • 1
  • 14
  • 24
81
votes
5 answers

What is difference between MediatorLiveData and MutableLiveData in MVVM

I have searched a lot but not found the crystal clear answer for the questions: What is the difference between MediatorLiveData and MutableLiveData? What are the suitable condition to use either of them.
Lalit Kushwah
  • 3,180
  • 4
  • 21
  • 36
79
votes
11 answers

What is the difference between map() and switchMap() methods?

What is the difference between those 2 methods of the LiveData class? The official doc and tutorial are pretty vague on that. In the map() method the first parameter called source but in the switchMap() it called trigger. What's the rationale behind…
78
votes
6 answers

How and where to use Transformations.switchMap?

In recent Android Architecture Components library released by Google, we have two static functions in the Transformations class. While the map function is straight forward and easily understandable, I am finding it hard to properly understand the…
72
votes
4 answers

Paging library - Boundary callback for network + db with API taking page and size

Short question: What is the correct way to handle database + network on the Paging library from Architecture components, using an API that uses page + size to load a new page and the BoundaryCallback class? Research and explanation Currently the…
72
votes
13 answers

Why LiveData observer is being triggered twice for a newly attached observer

My understanding on LiveData is that, it will trigger observer on the current state change of data, and not a series of history state change of data. Currently, I have a MainFragment, which perform Room write operation, to change non-trashed data,…
Cheok Yan Cheng
  • 49,649
  • 117
  • 410
  • 768
71
votes
7 answers

MutableLiveData with initial value

How I can initialize MutableLiveData with initial value? I'm looking for something like: val text = MutableLiveData("initial value")
Francis
  • 4,517
  • 3
  • 34
  • 54
67
votes
7 answers

How to clear LiveData stored value?

According to LiveData documentation: The LiveData class provides the following advantages: ... Always up to date data: If a Lifecycle starts again (like an activity going back to started state from the back stack) it receives the latest location…
Kamer358
  • 2,322
  • 2
  • 12
  • 16
64
votes
6 answers

LiveData update on object field change

I'm using Android MVVM architecture with LiveData. I have an object like this public class User { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void…
58
votes
4 answers

Live Data: Candidate resolution will be changed soon

In same cases Android studio displays warning message when I call observe method of LiveData object viewModel.emailValidationResult.observe(viewLifecycleOwner, {onEmailChanged(it)}) Candidate resolution will be changed soon, please use fully…
Vahe Gharibyan
  • 2,725
  • 1
  • 21
  • 30
1
2 3
99 100