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
36
votes
6 answers

getViewLifecycleOwner() in DialogFragment leads to crash

I use DialogFragment (onCreateDialog) and ViewModel for it. But, when I try to pass getViewLifecycleOwner() to the LiveData::observe method I get the error below: java.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when…
Alex.Marynovskyi
  • 1,264
  • 3
  • 16
  • 22
36
votes
9 answers

LiveData with shared preferences

I have a settings screen where I am setting some values. When I set those values it gets saved in shared preferences and these values are needed in my request to the network api call as parameters. Now I can use a listener for shared preferences in…
nick.tdr
  • 4,627
  • 4
  • 28
  • 41
36
votes
5 answers

update RecyclerView with Android LiveData

There are many examples how to push new list to adapter on LiveData change. I'm trying to update one row (e.g number of comments for post) in the huge list. It would be stupid to reset whole list to change only one field. I am able to add observer…
Andrew Matiuk
  • 924
  • 1
  • 7
  • 21
35
votes
2 answers

Refreshing MutableLiveData of list of items

I'm using LiveData and ViewModel from the architecture components in my app. I have a list of items that is paginated, I load more as the user scrolls down. The result of the query is stored in a MutableLiveData> When I do the…
Francesc
  • 25,014
  • 10
  • 66
  • 84
35
votes
7 answers

Android Room : LiveData callback of update insert?

I have a Simple DAO including CRUD function FeedEntryDAO.java @Dao public interface FeedEntryDAO { @Query("SELECT * FROM feedEntrys") LiveData> getAll(); @Query("SELECT * FROM feedEntrys WHERE uid = :uid LIMIT 1") …
34
votes
3 answers

MutableLiveData is null in JUnitTest

I want to write a unit test. Therefore I need MutableLiveData. I started with a very basic test for setup but I cannot instantiate a MutableLiveData object. I is always null when I run the test. Do I have to mock anything? Any suggestions?…
Kewitschka
  • 1,445
  • 1
  • 21
  • 35
33
votes
8 answers

Coroutines - unit testing viewModelScope.launch methods

I am writing unit tests for my viewModel, but having trouble executing the tests. The runBlocking { ... } block doesn't actually wait for the code inside to finish, which is surprising to me. The test fails because result is null. Why doesn't…
Prem
  • 3,373
  • 7
  • 29
  • 43
33
votes
5 answers

Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change

I found that the LiveData returned by Dao will call its observer whenever the row is updated in DB, even if the LiveData value is obviously not changed. Consider a situation like the following example : Example entity @Entity public class User { …
reTs
  • 1,808
  • 1
  • 13
  • 26
31
votes
2 answers

Room - LiveData observer does not trigger when database is updated

I am trying to find out in the code below, why is it that Room's LiveData observable does not give me new shifts once I populate the database with new data. This is put on my activity's onCreate method: shiftsViewModel =…
29
votes
3 answers

Is SingleLiveEvent actually part of the Android Architecture Components Library?

I have been using the SingleLiveData class which can be found here. My questions are: Is SingleLiveData is actually part of the Android Architecture Components? Is it a good idea to use it?
28
votes
8 answers

java.lang.NoSuchMethodError after upgrading Jetpack Compose to 1.0.0‑beta07

I'm getting the following error running observeAsState on a LiveData object after I upgraded Jetpack Compose to 1.0.0‑beta07. java.lang.NoSuchMethodError: No interface method startReplaceableGroup(ILjava/lang/String;)V in class…
Vahid
  • 1,829
  • 1
  • 20
  • 35
28
votes
2 answers

Flow onEach/collect gets called multiple times when back from Fragment

I'm using Flow instead of LiveData to collect data in my Fragment. In Fragment A I observe (or rather collect) the data in my fragment`s onViewCreated like this: lifecycleScope.launchWhenStarted { availableLanguagesFlow.collect { …
Andrew
  • 2,438
  • 1
  • 22
  • 35
28
votes
1 answer

Where to put viewModel Observers in a dialogFragment?

For fragments it is advised to put liveData observers in the onActivityCreated method. This works fine for fragments, but when I apply this to a dialogFragment I get the following error: java.lang.IllegalStateException: Can't access the Fragment…
KvdLingen
  • 1,230
  • 2
  • 14
  • 22