title: Shared ViewModel in scope of parent fragment using Navigation Component
body: <p>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.</p>
  
  <p><a href="https://i.stack.imgur.com/1BYHa.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1BYHa.png" alt="Fragments hierarchy"></a></p>
  
  <p>Expected: 
  I would like to share the same instance of fragment C ViewModel with fragment X and Y. </p>
  
  <p>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.</p>
  
  <p>What I tried already:
  In child viewModels I use this:</p>
  
  <pre><code>        val viewModel: ParentViewModel =
          ViewModelProvider(findNavController().getViewModelStoreOwner(R.id.parent_graph)).get(
              ParentViewModel::class.java
          )
  </code></pre>
  
  <p>In parent viewModel I use this: </p>
  
  <pre><code>    val viewModel by viewModels&lt;ParentViewModel&gt;()
  </code></pre>
  
  <p>I've also tried to inject the viewModel using Koin sharedViewModel with scope of fragment:</p>
  
  <pre><code>val viewModel by sharedViewModel&lt;ParentViewModel&gt;(from = { parentFragment!! })
  </code></pre>
  
  <p>Also no luck.</p>
  
  <p>Is it possible or maybe it is a bug in navigation library?</p>