title: Viewpager2 with fragments and Jetpack navigation: Restore fragments instead of recreating them
body: <p>I have a <code>Viewpager2</code> inside a <code>Fragment</code> (lets call it <code>HomeFragment</code>). That <code>Viewpager</code> itself also contains <code>Fragments</code>. When I navigate away from the <code>HomeFragment</code> its view will be destroyed and when I navigate back the view will be recreated. Now I set the adapter of the <code>Viewpager2</code> in the <code>HomeFragment</code> during <code>onViewCreated()</code>. Therefore the adapter will be recreated when I navigate back to the <code>HomeFragment</code>, which also recreates all <code>Fragments</code> in the <code>Viewpager2</code> and the current item is reset to 0. If i try to re-use the adapter that I instantiated on the first creation of the <code>HomeFragment</code>i get an exception, because of this check inside of the <code>FragmentStateAdapter</code>:</p>
  
  <pre><code>public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
          checkArgument(mFragmentMaxLifecycleEnforcer == null);
  </code></pre>
  
  <p>Does anybody have an idea how I can prevent recreating everything when navigating back? Otherwise this is a pretty big performance overhead and hinders my UX.</p>