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
1
vote
2 answers

Is there any way to make retrofit2 return LiveData

Is there any way to make retrofit2 return livedata instead of Observable ? If it's possible how we can implement this approach? If it's not possible, Is it recommanded to make retrofit return Observable then convert it to livedata?
1
vote
1 answer

How to validate the value in EditText with the value of current item's position in ViewPager using the observer?

I am using the ViewModel and Livedata. I tried getting the current position of ViewPager in ViewModel by using the following code
1
vote
2 answers

Not updating ViewModel after Retrofit CREATE request

I'm using the Retrofit client to perform HTTP requests in a simple Android project. When I send a CREATE request, the item is successfully created in the remote SQLite database, but the ViewModel still contains the old list. When I restart my…
1
vote
2 answers

Lifecycle state management with Room live queries

I have a view with three states: sealed class MainState(val movieList: List) { class Loading(movies: List = emptyList()) : MainState(movies) class Success(movies: List) : MainState(movies) class Error(val throwable:…
saiedmomen
  • 1,401
  • 16
  • 33
1
vote
1 answer

Listening socket.io events and updating LiveData (a class of android.arch.lifecycle)in application class android

I am working on a chat application and Currently I am listening and emitting socket events in an Activity. Now i want to listen all the socket events even when chat activity is not in foreground as i want to store all the messages from different…
Awadesh
  • 3,530
  • 2
  • 20
  • 32
1
vote
1 answer

Data not set in RecyclerView using AndroidViewModel with Volley in Android

I am using AndroidViewModel in application.But when my application is opened the method in ViewModelclass not called and data is not set on RecyclerView.This is code for my Model class public class Foodie { private String name; public…
1
vote
3 answers

MediatorLiveData sources don't update during testing

I have a MediatorLiveData living in my viewmodel that is supposed to react to LiveData emissions from the model layer, taking actions and updating its listeners when necessary. For some reason, the sources don't update during testing. class…
FutureShocked
  • 779
  • 1
  • 10
  • 26
1
vote
1 answer

How implement a cart in clean architecture?

I have cart in my app and store productId and count in Sqlite db (Wraps with Room). When fetching the productList from the server, in Data layer inject count of product to the received list and then pass to other layers to show on screen (etc.…
beigirad
  • 4,986
  • 2
  • 29
  • 52
1
vote
1 answer

Should I share my ViewModel across two fragments?

I'm attempting to follow Android best practices and use the latest recommended architecture components. You can see my attempt so far here: https://github.com/randroid88/TodayILearned Right now the app's features are very limited. It has a Room db…
1
vote
1 answer

How to modify LiveData from a database and forward it to the view

I'm getting a LiveData> from a database in my view model. But I have to add some Foo-objects to the list, before I can forward them to the view. I'm using the Room API to get access to a database. I'm using the recommended encapsulation with a Dao,…
Naryxus
  • 55
  • 1
  • 6
1
vote
0 answers

Android architecture components: LiveData as Service observer

This is rather an am I doing this correctly question. I have a MediaPlayer service to play audio. Due to few requirements, it is a callback hell with too many listeners. For example: Currently, I have an audio route button to toggle audio routing…
1
vote
1 answer

Loading data in splash activity and share that data or ViewModel to next activity in MVVM

In my app I need to load a feed of some data from server(using retrofit) that is loaded in a dedicated viewmodel. What happens currently is that the viewmodel calls the repository in its constrcutor, and then once the data is fetched , the…
BVtp
  • 2,308
  • 2
  • 29
  • 68
1
vote
1 answer

how to communicate to activity/fragment from repository class for web service in MVVM architecture

I am new to MVVM architecture and i just want to know how to communicate between repository class and the UI (activity/fragment) class.I came across with live data which is doing this job for updating same entities from both (remote and room…
1
vote
1 answer

Reacting to LiveData change within same ViewModel

I have a base ViewModel extended by other ViewModels: abstract class BaseViewModel : ViewModel() { protected val _state = MutableLiveData() protected val state: LiveData = _state ... } I'd like to be able to react…
liminal
  • 1,144
  • 2
  • 13
  • 24
1
vote
0 answers

How to handle multiple network statuses by LiveData on MVVM pattern

I have a problem with MVVM pattern and LiveData. I have a list of questions in recyclerView, these questions can be answered by user. I use Retrofit to connect with remote server and local db to store my data on device (I am considering Room, Realm…