update RecyclerView with Android LiveData

Like @Lyla said, you should observe the whole list as LiveData in Fragment or Activity, when receive changes, you should set the whole list to the adapter by DiffUtil. Fake code: PostViewModel { LiveData<List<Post>> posts; // posts comes from DAO or Webservice } MyFragment extends LifecycleFragment { PostAdapter postAdapter; … void onActivityCreated() { … postViewModel.posts.observer(this, … Read more

getViewLifecycleOwner() in DialogFragment leads to crash

This happens because of how the DialogFragment is created. If you use onCreateDialog() than a slightly different lifecycle is used for this type of Fragment. The onCreateView() will not be used, thus the viewLifecycleOwner for this Fragment won’t be initialized. As a workaround for this, you can use the Fragment instance as the owner for … Read more

LiveData with shared preferences

The following awesome piece of code is LiveData Implementation of SharedPreference. It works perfectly. package com.chargingwatts.chargingalarm.util.preference; import android.arch.lifecycle.LiveData import android.content.SharedPreferences abstract class SharedPreferenceLiveData<T>(val sharedPrefs: SharedPreferences, val key: String, val defValue: T) : LiveData<T>() { private val preferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key -> if (key == this.key) { value = getValueFromPreferences(key, defValue) } } abstract … Read more

Using LiveData as state inside Jetpack @Compose functions

Here is another way to observe live data using state. There is an extension function for this just include it. add gradle dependency below one: implementation ‘androidx.compose.runtime:runtime-livedata:1.0.0-beta01’ now just convert your regular LiveData to State for example. val breedItems by doggoViewModel.liveBreedData().observeAsState()

LiveData.getValue() returns null with Room

On the next line I am checking sections.getValue() which is always giving me null although I have data in the DataBase and later I am getting the value in the onChanged() method. This is normal behavior, because queries that return LiveData, are working asynchronously. The value is null at that moment. So calling this method … Read more

LiveData prevent receive the last value when start observing

I`m using this EventWraper class from Google Samples inside MutableLiveData /** * Used as a wrapper for data that is exposed via a LiveData that represents an event. */ public class Event<T> { private T mContent; private boolean hasBeenHandled = false; public Event( T content) { if (content == null) { throw new IllegalArgumentException(“null values … Read more

LiveData prevent receive the last value when start observing

I`m using this EventWraper class from Google Samples inside MutableLiveData /** * Used as a wrapper for data that is exposed via a LiveData that represents an event. */ public class Event<T> { private T mContent; private boolean hasBeenHandled = false; public Event( T content) { if (content == null) { throw new IllegalArgumentException(“null values … Read more

How and where to use Transformations.switchMap?

In the map() function LiveData userLiveData = …; LiveData userName = Transformations.map(userLiveData, user -> { return user.firstName + ” ” + user.lastName; // Returns String }); everytime the value of userLiveData changes, userName will be updated too. Notice that we are returning a String. In the switchMap() function: MutableLiveData userIdLiveData = …; LiveData userLiveData = … Read more