Anyone know how to share a ViewModel between a par...
# koin
a
Anyone know how to share a ViewModel between a parent fragment and child fragments (specifically in a ViewPager2) using delegated properties with Koin? I am only familiar with 
by sharedViewModel()
 (which is shared with the parent activity, not parent fragment) and 
by viewModel()
 (which I believe creates a new ViewModel instance for the child class). I'm using 
by viewModels({ requireParentFragment() })
 right now, which works fine, but it's not using Koin. I looked at this article, which suggested 
by viewModel(owner = { ViewModelOwner.Companion.from(requireParentFragment().viewModelStore) })
, but that doesn't work anymore. Similarly, this article suggested 
by sharedViewModel(from = { requireParentFragment() })
, but the 
from
 keyword doesn't exist anymore.
m
just realized I was also using the androidx method and not koin…
Copy code
sharedViewModel(owner = {
  ViewModelOwner.from(requireParentFragment())
})
might work?
a
Thanks, I'll try that
188 Views