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 observing in Fragment

If observing from an Activity you can observe on onCreate() and use this for the LifecycleOwner as stated here: If you have a lifecycle-aware component that is hooked up to the lifecycle of your activity it will receive the ON_CREATE event. The method annotated with @OnLifecycleEvent will be called so your lifecycle-aware component can perform … Read more

What is lifecycle observer and how to use it correctly?

You can use ProcessLifecycleOwner to get your Application’s LifeCycle and to add a class as an observer of these events. You can implement LifecycleObserver in your Application Class: public class MyApplication extends MultiDexApplication implements LifecycleObserver @Override public void onCreate() { super.onCreate(); ProcessLifecycleOwner.get().getLifecycle().addObserver(this); } // Add these Lifecycle methods to observe when your app goes into … Read more

Android LiveData – how to reuse the same ViewModel on different activities?

When you call ViewModelProviders.of(this), you actually create/retain a ViewModelStore which is bound to this, so different Activities have different ViewModelStore and each ViewModelStore creates a different instance of a ViewModel using a given factory, so you can not have the same instance of a ViewModel in different ViewModelStores. But you can achieve this by passing … Read more

MediatorLiveData or switchMap transformation with multiple parameters

Source : https://plus.google.com/+MichielPijnackerHordijk/posts/QGXF9gRomVi To have multiple triggers for switchMap(), you need to use a custom MediatorLiveData to observe the combination of the LiveData objects – class CustomLiveData extends MediatorLiveData<Pair<String, Integer>> { public CustomLiveData(LiveData<String> code, LiveData<Integer> nbDays) { addSource(code, new Observer<String>() { public void onChanged(@Nullable String first) { setValue(Pair.create(first, nbDays.getValue())); } }); addSource(nbDays, new Observer<Integer>() { … 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 to combine two live data one after the other?

You can use my helper method: val profile = MutableLiveData<ProfileData>() val user = MutableLiveData<CurrentUser>() val title = profile.combineWith(user) { profile, user -> “${profile.job} ${user.name}” } fun <T, K, R> LiveData<T>.combineWith( liveData: LiveData<K>, block: (T?, K?) -> R ): LiveData<R> { val result = MediatorLiveData<R>() result.addSource(this) { result.value = block(this.value, liveData.value) } result.addSource(liveData) { result.value = … Read more