Koin: Share fragment scoped view model with child fragments

Created on 9 Oct 2018  路  6Comments  路  Source: InsertKoinIO/koin

Describe the bug
With reference to #209 I have a parent fragment where I've declared:

class ParentFragment : Fragment() {
    val viewModel by viewModel<ParentViewModel>()
}

Within this fragment lies a nested navHostFragment which navigates to other child fragments like:

class FirstChildFragment : Fragment() {
    private val parentViewModel by sharedViewModel<ParentViewModel>(from = { parentFragment!! })
    private val viewModel by viewModel<FirstChildViewModel>()
}

And

class SecondChildFragment : Fragment() {
    private val parentViewModel by sharedViewModel<ParentViewModel>(from = { parentFragment!! })
    private val viewModel by viewModel<SecondChildViewModel>()
}

The instance of parentViewModel I get in the child fragments are the same (as expected), but they are not the same instance as the one in the parent fragment. Is that the expected behaviour?

How can a parent fragment and it's children all share the same view model instance, but one that is not scoped at the activity level as you would get if you _just_ used sharedViewModel.

To Reproduce
N/A

Expected behavior
The view model instance of the parent fragment and all the child fragments should be the same instance.

Koin project used and used version (please complete the following information):
koin-androidx-viewmodel version 1.0.1

Additional moduleDefinition

viewModel { ParentViewModel() }
viewModel { FirstChildViewModel() }
viewModel { SecondChildViewModel() }

@fredy-mederos tagging you here as well.

question

Most helpful comment

If you are using a nested navFragment then your child fragments aren't in the fisrt level.
The parenting is: ParentFragment -> NavFragment -> ChildFragment
Try this code in your child fragments:
private val parentViewModel by sharedViewModel<ParentViewModel>(from = { parentFragment?.parentFragment })

All 6 comments

If you are using a nested navFragment then your child fragments aren't in the fisrt level.
The parenting is: ParentFragment -> NavFragment -> ChildFragment
Try this code in your child fragments:
private val parentViewModel by sharedViewModel<ParentViewModel>(from = { parentFragment?.parentFragment })

That worked! Thank you.

I had the same issue but with a viewpager.

I have this in my HomeFragment:
private val viewModel: HomeViewModel by viewModel()

Then, in that fragment, I have a viewpager that holds fragments.

I've tried this in the PageFragment (child fragment held by the viewpager):
private val homeViewModel: HomeViewModel by sharedViewModel(from = { parentFragment!! })

and this:
private val homeViewModel: HomeViewModel by sharedViewModel(from = { parentFragment?.parentFragment!! })

they both return a KotlinNullPointerException

@Edorin9 did you find a solution? Facing the same issue.

@Edorin9 @michaelbukachi
FragmentA:
create and add FragmentB

I have no idea why parentFragment is always null before and after onAttach.

I solve this by passing fragmentA as a parameter to fragmentB

@Edorin9 did you find a solution? Facing the same issue.

As far as I remember, my issue back then was caused by using supportFragmentManager instead of childFragmentManager on the child fragment (you should use childFragmentManager on PageFragment). I'm not really sure if that was it. So, post codes of the involved files if you need further help.

Sorry for not immediately posting an update with my problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hkelidari picture hkelidari  路  3Comments

AHarazim picture AHarazim  路  3Comments

LukasAnda picture LukasAnda  路  3Comments

iRYO400 picture iRYO400  路  3Comments

sankarsana picture sankarsana  路  4Comments