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

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

When to use MutableLiveData and LiveData

LiveData has no public method to modify its data. LiveData<User> getUser() { if (userMutableLiveData == null) { userMutableLiveData = new MutableLiveData<>(); } return userMutableLiveData } You can’t update its value like getUser().setValue(userObject) or getUser().postValue(userObject) So when you don’t want your data to be modified use LiveData If you want to modify your data later use … Read more

Paging library – Boundary callback for network + db with API taking page and size

The documentation has this to say on the issue: If you aren’t using an item-keyed network API, you may be using page-keyed, or page-indexed. If this is the case, the paging library doesn’t know about the page key or index used in the BoundaryCallback, so you need to track it yourself. You can do this … Read more

Use viewLifecycleOwner as the LifecycleOwner

Why I get this error? Lint is recommending that you use the lifecycle of the fragment’s views (viewLifecycleOwner) rather than the lifecycle of the fragment itself (this). Ian Lake and Jeremy Woods of Google go over the difference as part of this Android Developer Summit presentation, and Ibrahim Yilmaz covers the differences in this Medium … Read more

How to clear LiveData stored value?

Update There are actually a few ways to resolve this issue. They are summarized nicely in the article LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case). This is written by a fellow Googler who works with the Architecture Components team. TL;DR A more robust approach is to use an Event wrapper class, which … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)