13

I am using Koin for injecting viewModel into fragment. My app is single activity. I need that sharedViewModel only in servisFragment and partFragment. I would like to clear that viewModel from Activity after navigation marked with red.

How can I do that?

navigation

Code for injecting viewModel

    private val servisViewModel by sharedViewModel<ServisViewModel>()

Koin sharedViewModel

inline fun <reified T : ViewModel> Fragment.sharedViewModel(
    name: String? = null,
    noinline from: ViewModelStoreOwnerDefinition = { activity as 
    ViewModelStoreOwner },
    noinline parameters: ParametersDefinition? = null
) = lazy { getSharedViewModel<T>(name, from, parameters) }

Thank you for any help.

solaza
  • 1,251
  • 1
  • 17
  • 30
  • In the traditional nested fragment, we can use `by sharedViewModel(from = { parentFragment as ViewModelStoreOwner})` to bind ViewModel lifecycle to parent fragment lifecycle. But in the navigation component, I have not idea how to get the parent fragment instance. – Kiwi Lin Jul 11 '19 at 09:49
  • I managed to bind with Koin and code above but I don't know how to clear/unboud it and if thats is even needed. – solaza Jul 11 '19 at 10:58
  • You don't need to clear just like `by viewModel()` can auto clear when the fragment destory. – Kiwi Lin Jul 11 '19 at 11:06
  • Added code for sharedViewModel. Does it get cleared when fragment is destroyed even if it's using activity as ViewModelStoreOwner like in code above? – solaza Jul 11 '19 at 11:12
  • Do you use the navigation component? If true, you can not get the parent fragment by calling `getParentFragment()`. It always returns `NavHostFragment` not your `servisFragment`. – Kiwi Lin Jul 11 '19 at 11:13
  • Yes I am using it. Why do I need parent fragment? As I said I have single Activity Application and all fragments inside that Activity. – solaza Jul 11 '19 at 11:21
  • `I need that sharedViewModel only in servisFragment and partFragment`. In your graph, `partFragment` is the child fragment and `servisFragment` is the parent fragment. So, if we can bind `sharedViewModel` to parent fragment, `sharedViewModel` will be clear when you navigate the red path. – Kiwi Lin Jul 11 '19 at 11:27
  • Take a look at https://github.com/InsertKoinIO/koin/issues/183 and its reply – Kiwi Lin Jul 11 '19 at 11:36
  • When I use `findNavController().navigate(ServisFragmentDirections.Action_servisFragment_to_menuFragment())` to get back to `menuFragment` and then go again to `servisFragment` everything is still in it (Data not cleared). – solaza Jul 11 '19 at 11:38
  • Check your parent fragment in `partFragment`. If you bind the wrong parent, the ViewModel will not be clear – Kiwi Lin Jul 11 '19 at 11:41
  • As you said before `getParentFragment()` returns `NavHostFragment`. Is there any way to get `servisFragment`? – solaza Jul 11 '19 at 11:53

2 Answers2

4

if you need to clear all viewModels from that Fragment try this in your Fragment

viewModelStore.clear()

if you need to clear concrete ViewModel try this

getViewModelStore(ViewModelParameters(...)).clear()
Hayk Melkonyan
  • 2,110
  • 4
  • 16
  • 22
2

If you are using koin to inject, in the onDestoy of the fragment you should use

requireActivity().viewModelStore.clear()

because viewModelStore directly from fragment will return none to clear

But the problem with this is that it will clear ALL the view model scoped within this ViewModelStore. So you won't have control of which ViewModel to clear.