title: How to switch List fragment and Detail fragment in view pager tab?
body: <p>Im trying to implement switching of fragments inside first tab of view pager.I tried with approach found here <a href="https://github.com/danilao/fragments-viewpager-example" rel="nofollow noreferrer">link</a>  which uses root fragment in first tab and then switches child fragments. But when in second child fragment screen rotation occurs app switches back to first child fragment and besides that it seems like a hack.</p>
  <p>I tried searching for other approaches but found dozens of answers that all seems messy.
  Explanation from <a href="https://guides.codepath.com/android/viewpager-with-fragmentpageradapter#replacing-fragments-inside-viewpager" rel="nofollow noreferrer">here</a>  seems right but i dont fully understand it.Here is the explanation and code from that link:</p>
  <p>This  approach is to switch the fragment that displays inside the <code>FragmentPagerAdapter</code> by overriding the <code>getItem(...)</code> method as well as <code>getItemPosition(Object object)</code> which is invoked every time you call <code>viewPager.getAdapter().notifyDataSetChanged()</code>. For example:</p>
  <pre><code>public static class MyPagerAdapter extends FragmentStatePagerAdapter {
  // Return a different fragment for position based on additional state tracked in a member variable
  @Override
  public Fragment getItem(int position) {
      // For a given position, return two different potential fragments based on a condition
  }
  
  // Force a refresh of the page when a different fragment is displayed
  @Override
  public int getItemPosition(Object object) {
      // this method will be called for every fragment in the ViewPager
      if (object instanceof SomePermanantCachedFragment) {
          return POSITION_UNCHANGED; // don't force a reload
      } else {
          // POSITION_NONE means something like: this fragment is no longer valid
          // triggering the ViewPager to re-build the instance of this fragment.
          return POSITION_NONE;
      }
  }
  }
  </code></pre>
  <p>Lets say i have <code>ListFragment</code> and <code>DetailFragment</code> that should switch in first tab.</p>
  <ol>
  <li>Where will i call <code>viewPager.getAdapter().notifyDataSetChanged()</code>  ?</li>
  <li>(...based on additional state tracked in a member variable) how do i implement this?</li>
  <li><code>SomePermanantCachedFragment</code>   refers to <code>ListFragment</code> in my case?</li>
  </ol>
  <p>I also tried searching github for example but with no success...</p>