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

2899 questions
192
votes
10 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
  • 57,232
  • 27
  • 203
  • 212
174
votes
9 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
  • 4,255
  • 5
  • 23
  • 41
127
votes
8 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…
123
votes
10 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
  • 4,043
  • 4
  • 24
  • 36
119
votes
17 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
  • 2,390
  • 2
  • 20
  • 29
108
votes
14 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…
106
votes
4 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…
103
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…
98
votes
4 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,861
  • 5
  • 25
  • 40
96
votes
5 answers

Kotlin Flow vs Android LiveData

I have some questions about Kotlin Flow I can observe LiveData from multiple Fragments. Can I do this with Flow? If yes then how? We can have multiple LiveData from a single LiveData using map& switchMap. Is there any way to have multiple Flow from…
zoha131
  • 1,758
  • 2
  • 16
  • 18
96
votes
14 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
  • 47,586
  • 132
  • 466
  • 875
90
votes
8 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
  • 6,788
  • 5
  • 47
  • 64
89
votes
6 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
  • 5,277
  • 4
  • 36
  • 47
79
votes
7 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…
Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
77
votes
11 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,430
  • 2
  • 16
  • 17
1
2 3
99 100