title: How to use a shared ViewModel, and avoid reusing the same instance of it every time with Navigation Component
body: <p>My app consists of one single <code>Activity</code> with multiple <code>Fragments</code>, following the "single Activity app model", so that I can implement properly navigation using the <code>Navigation Component</code> in Android <code>jetpack</code>.</p>
  
  <p>Most of my screens (<code>Fragments</code>) are standalone and don't depend on each other, hence they use their own <code>ViewModel</code></p>
  
  <p>Some features require navigation involving more than one <code>Fragment</code>. As those features share data between them that are passed back and forth through the <code>Fragments</code>, I use a <strong>shared ViewModel</strong> (as recommended by Google). 
  I need to use the same instance of the shared <code>ViewModel</code> in all the associated <code>Fragments</code>, as I need the <code>Fragments</code> share the state of the shared <code>ViewModel</code>.</p>
  
  <p>To use the same instance of the <code>ViewModel</code> in these associated <code>Fragments</code>, I need to create the <code>ViewModel</code> using the parent <code>Activity</code> (not the <code>Fragment</code>) when getting the <code>ViewModel</code> from the <code>ViewModelProviders</code>:</p>
  
  <pre><code>val viewModel = ViewModelProviders.of(
              parentActivity, factory.create()
          ).get(SharedViewModel::class.java)
  </code></pre>
  
  <p>This works, however, It produces one problem: 
  when navigating a consecutive times to the first <code>Fragment</code> that requires the shared <code>ViewModel</code>, <code>ViewModelProviders.of()</code> will return the same instance of the <code>ViewModel</code> as before: The <code>ViewModel</code> is being shared between the <code>Fragments</code>, but also between different navigations to the feature implemented like this.</p>
  
  <p>I understand why this is happening (Android is storing the ViewModel in a <code>map</code>, which is being used when requesting the <code>ViewModel</code> with <code>ViewModelProviders.of()</code>), but I don't know how I am expected to implement the "shared ViewModel pattern" properly.</p>
  
  <p>The only workarounds I see are:</p>
  
  <ul>
  <li>Create a different <code>Activity</code> for the feature that uses the <code>Fragment</code> with a shared <code>ViewModel</code> </li>
  <li>Use nested <code>Fragments</code>, and use common parent <code>Fragment</code> for the feature that uses the <code>Fragment</code> with a shared <code>ViewModel</code> </li>
  </ul>
  
  <p>With these two options, I would be able to create a <code>ViewModel</code> that will be shared between the <code>Fragments</code> intervening in the feature, and will be different each time I navigate to the feature.</p>
  
  <p>The problem I see here is that this seems to be that against the fundamentals of the <code>Navigation Component</code> and the single <code>Activity</code> app. 
  Each feature implemented this way will need to have a different navigation graph, as they will use a different navigation host. This would prevent me from using some of the nice features of <code>Navigation Component</code>.</p>
  
  <p>What is the proper way to implement what I want? 
  Am I missing anything, or is it the way it is?</p>
  
  <p>Before <code>Navigation Component</code> I would use different <code>Activities</code> and <code>Fragments</code> and use <code>Dagger</code> scopes associated with the <code>Activity</code>/<code>Fragment</code> to achieve this. But I'm not sure what's the best way of implementing this with just one Activity`</p>