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
74
votes
4 answers

Use viewLifecycleOwner as the LifecycleOwner

I have a fragment: class MyFragment : BaseFragment() { // my StudentsViewModel instance lateinit var viewModel: StudentsViewModel override fun onCreateView(...){ ... } override fun onViewCreated(view: View,…
user842225
  • 5,445
  • 15
  • 69
  • 119
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…
62
votes
8 answers

When to use MutableLiveData and LiveData

when to use MutableLiveData and LiveData means the area of using the methods : MutableLiveData getUser() { if (userMutableLiveData == null) { userMutableLiveData = new MutableLiveData<>(); } return…
57
votes
13 answers

How to combine two live data one after the other?

I have next use case: User comes to registration form, enters name, email and password and clicks on register button. After that system needs to check if email is taken or not and based on that show error message or create new user... I am trying to…
56
votes
16 answers

LiveData prevent receive the last value when start observing

Is it possible to prevent LiveData receive the last value when start observing? I am considering to use LiveData as events. For example events like show message, a navigation event or a dialog trigger, similar to EventBus. The problem related to…
51
votes
5 answers

MediatorLiveData or switchMap transformation with multiple parameters

I am using Transformations.switchMap in my ViewModel so my LiveData collection, observed in my fragment, reacts on changes of code parameter. This works perfectly : public class MyViewModel extends AndroidViewModel { private final…
46
votes
2 answers

LiveData. Cannot assign to ‘value’: the setter is protected/*protected and package*/ for synthetic extension

I'm trying to implement a DB Observer with LiveData as described in the android documentation. As long as I'm programming in Kotlin I'm adapting the functions (originally written in Java) to it. When trying to save the data I find this…
kike
  • 4,255
  • 5
  • 23
  • 41
46
votes
4 answers

Android LiveData - how to reuse the same ViewModel on different activities?

Example ViewModel: public class NameViewModel extends ViewModel { // Create a LiveData with a String private MutableLiveData mCurrentName; public MutableLiveData getCurrentName() { if (mCurrentName == null) { …
user1209216
  • 7,404
  • 12
  • 60
  • 123
45
votes
6 answers

What is lifecycle observer and how to use it correctly?

I have read about new architectural components in Android. So, i wanted to ask what are lifecycle observers and why do we need them? In what cases it is useful? Thanks for your answer!
Nazarii Moshenskiy
  • 1,802
  • 2
  • 16
  • 35
44
votes
3 answers

LiveData observing in Fragment

As of 2019, I'm trying to follow a best practice on where to start observing LiveData in Fragments and if I should pass this or viewLifecycleOwner as a parameter to the observe() method. According to this Google official documentation, I should…
43
votes
13 answers

ViewModel onchange gets called multiple times when back from Fragment

I am working with Android architecture components. What i want is when user type "0" in Edittext and click on Button to replace Fragment with new one , and if type anything else post Toast error message. In Problem is when i back from new…
Nikolas Bozic
  • 1,071
  • 2
  • 13
  • 20
42
votes
8 answers

MutableLiveData not updating in UI

UPDATE: If i move to another fragment and return to this one the TextView gets updated... I am unable to get the MutableLiveData in the UI to update to a new value either by using setValue() or postValue(). I can get this to work by changing the…
DevinM
  • 1,112
  • 1
  • 12
  • 29
40
votes
7 answers

LiveData.getValue() returns null with Room

Java POJO Object public class Section { @ColumnInfo(name="section_id") public int mSectionId; @ColumnInfo(name="section_name") public String mSectionName; public int getSectionId() { return mSectionId; } …
38
votes
4 answers

Using LiveData as state inside Jetpack @Compose functions

I want to use a LiveData> to be the source of my state in a @Composable function. I cannot use the new @Model annotation, I have seen in this talk Link(at 32:06) it is possible to use LiveData, Flow, etc. by invoking the function…
36
votes
3 answers

Kotlin Flow vs LiveData

In the last Google I/O, Jose Alcerreca and Yigit Boyar told us that we should no longer use LiveData to fetch data. Now we should use suspend functions for one-shot fetches and use Kotlin's Flow to create a data stream. I agree that coroutines are…