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
27
votes
2 answers

Accessing BroadCastReceiver in Viewmodel

I am struggling in choosing the right way to pass data from broadcastReceiver to ViewModel and from there I pass data to my Repository and update LiveData. I use FCM push notifications and have local broadCastReceiver which uses ActivityLifecycle.…
27
votes
3 answers

Convert RxJava Observables To Live Data With Kotlin Extension Functions

I've been using alot of RxJava Observables converted to LiveData in my code using LiveDataReactiveStreams.fromPublisher() library. So I though of adding an extension function to the RxJava Observable to easily convert them to LiveData. These are my…
27
votes
1 answer

Get item by id in Room

I'm using Room + LiveData in my Android project. Following to Google Blueprints, I've implemented data layer of my application. This is how my Dao looks like: @Query("SELECT * FROM events WHERE id=:arg0") fun loadSingle(id: String):…
27
votes
1 answer

Observe LiveData from foreground service

I have a repository which holds the LiveData object and is used by both activity and a foreground service through a ViewModel. When I start observing from the activity everything works as expected. However observing from the service doesn't trigger…
Ilya Sosis
  • 352
  • 1
  • 4
  • 9
26
votes
3 answers

Unresolved reference: Transformations after upgrading lifecycle dependency

After upgrading the lifecycle dependency from 2.6.0-alpha04 to 2.6.0-beta01 I got Unresolved reference: Transformations and it can't import androidx.lifecycle.Transformations class. import androidx.lifecycle.Transformations ... var myList:…
Zain
  • 37,492
  • 7
  • 60
  • 84
26
votes
2 answers

Setting up LiveData observer in custom view without LifecycleOwner

I'm trying out the new Android Architecture components and have run into a road block when trying to use the MVVM model for a custom view. Essentially I have created a custom view to encapsulate a common UI and it's respective logic to use…
26
votes
10 answers

Error:Program type already present: android.arch.lifecycle.LiveData

When I press the run button in Android Studio, my app compiles but shows this error (redacted): Error:Program type already present: android.arch.lifecycle.LiveData (Full log) I've tried deleting the .gradle folder, then going to Build > Clean…
Edric
  • 24,639
  • 13
  • 81
  • 91
26
votes
5 answers

LiveData Observer not Called

I have an activity, TabBarActivity that hosts a fragment, EquipmentRecyclerViewFragment. The fragment receives the LiveData callback but the Activity does not (as proofed with breakpoints in debugging mode). What's weird is the Activity callback…
26
votes
4 answers

setValue and postValue on MutableLiveData in UnitTest

I try to test some methods on my Application but I get an error when calling mutablelivedata.postValue. Here is a snippet and the error message: @Test public void callStartScreenRepository(){ Observer userObserver = mock(Observer.class); …
ECommerce
  • 469
  • 1
  • 4
  • 9
25
votes
4 answers

Best practice: Runtime filters with Room and LiveData

I am working on a screen that shows the contents of a Room wrapped DB using a recycler. The adapter gets the LiveData from a ViewModel that hides the query call on the Room DAO object. So, the LiveData object is actually a ComputableLiveData object…
Oderik
  • 2,242
  • 2
  • 16
  • 25
25
votes
7 answers

Is it possible to enforce non-nullability of LiveData values?

Is there any way to enforce non-nullability of LiveData values? Default Observer implementation seems to have @Nullable annotation which forces an IDE to suggest that the value might be null and should be checked manually: public interface…
24
votes
5 answers

Room Dao LiveData as return type causing compile time error

I am using Room and implemented Dao that returns LiveData. It was working fine with below dependency added. implementation "androidx.room:room-runtime:2.1.0-alpha04" kapt "androidx.room:room-compiler:2.1.0-alpha04" But when I added new Room…
24
votes
2 answers

Cannot invoke observeForever on a background thread

I've been using an observeForever() method as described here to test Room and LiveData for a while, and it has worked flawlessly. But when I changed to Android Studio 3.2 (or if it was the androidx refactoring, not sure), that method suddenly…
24
votes
3 answers

How to make retrofit API call using ViewModel and LiveData

this is the first time I'm trying to implement MVVM architecture, and I'm a bit confused about the correct way to make an API call. Currently, I'm just trying to make a simple query from the IGDB API, and output the name of the first item in a log.…
Chao Li
  • 391
  • 1
  • 2
  • 13
23
votes
2 answers

Correct way to expose MutableLiveData as LiveData?

Consider the following ways to expose MutableLiveData: Method A class ThisViewModel : ViewModel() { private val _someData = MutableLiveData(true) val someData: LiveData get() = _someData } // Decompiled Kotlin…
user11566289