Search code examples
androidandroid-fragmentsviewmodelandroid-jetpackandroid-jetpack-navigation

Shared ViewModel in scope of parent fragment using Navigation Component


I am trying to use the same instance of ViewModel in Parent Fragment and its children, using Navigation Component. The hierarchy is as follows: Single Activity that has navigationHost. This host has 3 child fragments, A, B and C. The last fragment has also navigationHost with 2 fragments: X and Y. The below graph illustrates the hierarchy.

Fragments hierarchy

Expected: I would like to share the same instance of fragment C ViewModel with fragment X and Y.

Current: The ViewModel of fragment C is initialized twice: Once when fragment C is initialized and second time when fragment X is initialized. The Fragment X is set as a default destination in the fragment C nav graph. When I am changing the default destination to Y, the ViewModel is initialized in C and Y.

What I tried already: In child viewModels I use this:

        val viewModel: ParentViewModel =
        ViewModelProvider(findNavController().getViewModelStoreOwner(R.id.parent_graph)).get(
            ParentViewModel::class.java
        )

In parent viewModel I use this:

    val viewModel by viewModels<ParentViewModel>()

I've also tried to inject the viewModel using Koin sharedViewModel with scope of fragment:

val viewModel by sharedViewModel<ParentViewModel>(from = { parentFragment!! })

Also no luck.

Is it possible or maybe it is a bug in navigation library?


Solution

  • A NavHostFragment is a fragment itself, so your structure is actually

    Fragment C -> NavHostFragment -> Fragment X
                                  -> Fragment Y
    

    I.e., the parentFragment you get from Fragment X is not Fragment C - it is the NavHostFragment you added in between the two.

    Therefore if you want to get a ViewModel from Fragment C, you'd need to use requireParentFragment().requireParentFragment() - the parent of your NavHostFragment is Fragment C.