title: java.lang.IllegalStateException: Fragment no longer exists for key f1: index 3
body: <p>I want to understand this exception in order to implement a proper fix.</p>
  
  <p>There's a ViewPager and it uses a FragmentStatePagerAdapter to instantiate 2 fragments via getItem and <code>MyFragmentClass.newInstance(...)</code>.</p>
  
  <p>Adapter's getItem looks like this:</p>
  
  <pre><code>@Override
  public Fragment getItem(int position) {
      Fragment fragment = null;
  
      switch(position) {
          case 0:
              fragment = MyFragment2.newInstance(par1);
              break;
          case 1:
              fragment = MyFragment2.newInstance(par2, par3);
              break;
      }
      return fragment;
  }
  </code></pre>
  
  <p>Problem:</p>
  
  <p>When the activity is destroyed, and created again, the adapter is intantiated again, the fragments created again with <code>MyFragmentClass.newInstance(...)</code>... but then on this line:</p>
  
  <p><code>pager.setAdapter(adapter);</code></p>
  
  <p>I get the mentioned exception.</p>
  
  <p>I looked in the source where the exception is thrown, it's this:</p>
  
  <pre><code>@Override
  public Fragment getFragment(Bundle bundle, String key) {
      int index = bundle.getInt(key, -1);
      if (index == -1) {
          return null;
      }
      if (index &gt;= mActive.size()) {
          throw new IllegalStateException("Fragement no longer exists for key "
                  + key + ": index " + index);
      }
      Fragment f = mActive.get(index);
      if (f == null) {
          throw new IllegalStateException("Fragement no longer exists for key "
                  + key + ": index " + index);
      }
      return f;
  }
  </code></pre>
  
  <p>So a bundle is passed there, with some state which references my old fragments, but this doesn't correspond to the current state (<code>mActive</code>), and the exception is thrown.</p>
  
  <p>I don't understand what's the idea behind this, or which way I'm supposed to instantiate the fragments.</p>
  
  <p>I tried a trick I got from another context:</p>
  
  <p><code>pager.setOffscreenPageLimit(1);</code></p>
  
  <p>In order to avoid that the fragments are destroyed when they are off screen (in the case of 2 pages viewpager, although don't know if it works well with state adapter). But don't seems to be related, at least, it doesn't help, still get the same exception.</p>
  
  <p>Catching the exception leads to the pages being blank.</p>