Should I include LifecycleOwner in ViewModel?

No. If you wish to observe changes of some LiveData inside your ViewModel you can use observeForever() which doesn’t require LifecycleOwner. Remember to remove this observer on ViewModel‘s onCleared() event: val observer = new Observer() { override public void onChanged(Integer integer) { //Do something with “integer” } } … liveData.observeForever(observer); … override fun onCleared() { … 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

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

When is the viewmodel onCleared called

Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. ViewModels (VMs) are independent of configuration changes and are cleared when activity/fragment is destroyed. Following is the lifecycle of ViewModel from official site: Can the viewModel be shared with another Activity ? You shouldn’t do that with Activities. However fragments can share a ViewModel … Read more

Manually clearing an Android ViewModel?

Quick solution without having to use Navigation Component library: getActivity().getViewModelStore().clear(); This will solve this problem without incorporating the Navigation Component library. It’s also a simple one line of code. It will clear out those ViewModels that are shared between Fragments via the Activity

how to get viewModel by viewModels? (fragment-ktx)

Atlast we we have got stable version. After moving to implementation ‘androidx.fragment:fragment-ktx:1.1.0’ i faced another issue. ###Compiler Error: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 ###build.gradle (Module:app) compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 } kotlinOptions { jvmTarget = “1.8” } reference After … Read more

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