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
0 answers

LiveData and Transformations.switchMap crashing

I've got an app where I want to add a status of some sort, and if added, I want my UI to show a snackbar saying it was added. I've got a repository, where I add my status. If successful, I'll post the value back to my ViewModel. Since it's a…
Mikkel Larsen
  • 876
  • 2
  • 14
  • 26
1
vote
2 answers

How to change a live data instance in view model?

I am setting a room query in onCreate() which returns a live data instance that I observe in the following. viewModel.setmQueryMediaList(null, MediaUtils.DATE_TAKEN_KEY,…
fabi
  • 454
  • 1
  • 5
  • 12
1
vote
2 answers

Livedata mathematical operations

I have two livedatas. I need to take subtraction on them, but how to do it with two livedatas? I've created something like this, but this is no proper way because it doesn't refresh result always when I need it. …
beginner992
  • 659
  • 1
  • 9
  • 28
1
vote
1 answer

LiveData Observer for realmresults not getting triggered first time

I am going through Guide to app architecture and trying to implement MVVM and LiveData in one of my apps. I am using realm and I am using this to create a RealmLiveData as shown below class RealmLiveData(private val results:…
hushed_voice
  • 3,161
  • 3
  • 34
  • 66
1
vote
1 answer

ViewModel with several LiveData and UseCases

I am currently experimenting with making a viewmodel for Fragment. My approach is to use exactly one viewmodel for one fragment. I have several use cases for different scenarios ex. to fetch books, to get info about a book. All these use cases…
Atalyk
  • 495
  • 1
  • 5
  • 11
1
vote
1 answer

LiveData does not show data Instantly on opening app

The Issue I implemented the Android Architecture library and with this I am restoring data from Room Database, with MVVM (Model View View-Model) and whenever I use the observer to view LiveDada every time I start the app there is a noticeable delay…
1
vote
2 answers

How can I access to LiveData related object outside the observer?

I'm making an app that uses room and Livedata to store and display data, I can create and delete single items without the problem, But I'm struggling to create a process to update values. The problem is that I need to access an object related to…
Eduardo Corona
  • 1,262
  • 4
  • 20
  • 31
1
vote
1 answer

Android Databinding with LiveData - Validating multiple fields together

I am using two-way Databinding with LiveData inside a ViewModel to handle a sign-up form. As fields are filled out, there are fields that need to be evaluated together for validity (as well as the overall form), and only enable the Submit button…
1
vote
1 answer

Is it better to expose the LiveData object as a parameter of the ViewModel or rather returned by a member function call?

Usually MutableLiveData is used in the ViewModel and then the ViewModel only exposes immutable LiveData objects to the observers. https://developer.android.com/topic/libraries/architecture/livedata#update_livedata_objects Is it better to expose…
1
vote
0 answers

Android Room getById(id: Int) query returns a valid object when called from first fragment, but returns null when called from all the others

I am working on my school project, and I started getting this weird Room behavior. I have to admit, that everything used to work correctly, but after changing some things it stopped, and now it doesn't work even though I returned almost everything…
1
vote
1 answer

ViewModel + LiveData in Fragment, How to keep data of the ListItemFragment when navigate back from ItemDetailFragment

At the bottom in the onCreateView of ListItemFragment, i make a network call to get all items. like this: viewmodel.getAllItems() Then, observe the data to populate items to the RecyclerView. I'm using Navigation Architecture Component to navigate…
Tôn
  • 11
  • 2
1
vote
0 answers

Best approach to use DiffUtil with LIveData + Room Database?

I am using Room Database with LiveData , but my Local Database is updating too fast as per our requirement and at the same time i have to reload my recycler view .instead of calling notifyDataSetChanged() to adapter , i am trying to use DiffUtil ,…
Dhiru
  • 3,040
  • 3
  • 25
  • 69
1
vote
1 answer

Android Architecture Component(LiveData): How to clear all value from LiveData once it updates a textview

suppose I created mutableLivedata variable(an observer), and I updated a textview through it, now I wouldlike to clear value that was sent by livedata once it updates textview like I just created Livedata variable and its fresh in initial stage, it…
1
vote
2 answers

Searchview using keyword with Unsplash API

I'm calling in my app Unsplash API. I want to get photos based on the keyword. But I can't something is wrong in my Model or URL and I get this error : Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ I'm using Paging library…
android
  • 135
  • 1
  • 11
1
vote
2 answers

How it impact on memory if I create all View Models with Activity scope?

I'm using new Android architectural components : MVVM, LiveData, etc. As recommended by Android documentation as well as in many blogs, in order to share data between fragments, we should create SharedViewModel which will be accessed by each…