title: ViewPager2/Tabs problem with ViewModel state
body: <p>I am following the MVVM pattern - meaning I have a ViewModel for each Fragment.</p>
  
  <p>I added <em>two</em> tabs by using ViewPager2.</p>
  
  <p>My adapter looks like this:</p>
  
  <pre><code>@Override
  public Fragment createFragment(int position) {
      switch (position) {
          case 0:
              return new MergedItemsFragment();
          case 1:     
              return new ValidatedMergedItemsFragment();
      }
      return new MergedItemsFragment();
  }
  </code></pre>
  
  <p>The tabs are working. However, I noticed that the ViewModel of my MergedItemsFragment is behaving weirdly.
  Before I added tabs I navigated to the Fragment like this:</p>
  
  <pre><code>NavHostFragment.findNavController(this).navigate(R.id.action_roomFragment_to_itemsFragment);
  </code></pre>
  
  <p>When I left that fragment with <code>NavHostFragment.findNavController(this).popBackStack()</code> and later on returned to that fragment I would get a new empty ViewModel. <em>This was intended.</em></p>
  
  <p>With the new approach I am navigating with <code>return new MergedItemsFragment()</code>. When I leave that fragment and later on return I am getting a ViewModel that <strong>contains the old data</strong>. That is an issue because the old data is not relevant anymore because the User selected different data in another fragment.</p>
  
  <hr>
  
  <p>Update #1</p>
  
  <p>I realized that he actually keeps <strong>all</strong> the old <strong>Fragments</strong> in memory because the same print statements gets called multiple times. The times it is called increases with the amount of times I leave and return to that screen. So if I leave and return 10 times and rotate my device he will actually execute one line 10 times. <em>Any guesses how to implement Tabs/ViewPagers with Navigation Components in a manner that works with ViewModels?</em></p>
  
  <hr>
  
  <p>Update #2</p>
  
  <p>I set my ViewModels like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>viewModel = new ViewModelProvider(this, providerFactory).get(MergedItemViewModel.class)
  </code></pre>
  
  <p>I get the same results with:</p>
  
  <pre class="lang-java prettyprint-override"><code>viewModel = ViewModelProviders.of(this).get(MergedItemViewModel.class);
  </code></pre>
  
  <p>I bind the ViewModel in the Fragment itself. Therefore, <code>this</code> is the Fragment.</p>