title: FragmentStatePagerAdapter IllegalStateException: <MyFragment> is not currently in the FragmentManager
body: <p>I'm getting this on some cases, in onResume(), of an activity which uses a FragmentStatePagerAdapter. When using device's back button. Not always. Not reproducible.</p>
  
  <p>I'm using support package v4, last revision (8).</p>
  
  <p>Already searched with google, no success finding a useful answer.</p>
  
  <p>Looking in the source, it's thrown here: FragmentManager.java</p>
  
  <pre><code>@Override
  public void putFragment(Bundle bundle, String key, Fragment fragment) {
      if (fragment.mIndex &lt; 0) {
          throw new IllegalStateException("Fragment " + fragment
                  + " is not currently in the FragmentManager");
      }
      bundle.putInt(key, fragment.mIndex);
  }
  </code></pre>
  
  <p>But why is the index of fragment &lt; 0 there?</p>
  
  <p>The code instantiating the fragments:</p>
  
  <pre><code>@Override
  public Fragment getItem(int position) {
      Fragment fragment = null;
  
      switch(position) {
          case 0:
              fragment = MyFragment.newInstance(param1);
              break;
          case 1:
              fragment = MyFragment2.newInstance(param2, param3);
              break;
      }
      return fragment;
  }
  
  @Override
  public int getCount() {
      return 2;
  }
  </code></pre>