title: Android IllegalStateException: Fragment already added (only if onConfigChanged is called at launch)
body: <p>This error occurs only if I launch the app and immediately change the orientation during the launch phase.  onCreate gets called, then onResume gets called, then onConfigurationChanged gets called and I experience this error.  If I launch the app, wait a second and then change the orientation, everything works flawlessly.</p>
  
  <p>Where the FragmentPagerAdapter is created.  This is in the onCreate of the main activity.</p>
  
  <pre><code>mAdapter = new TabPagerAdapter(getFragmentManager());
  
  
  viewPager.setOffscreenPageLimit(25); //I want those fragments to not be destroyed
  viewPager.setAdapter(mAdapter);
  </code></pre>
  
  <p>And the displayFragment is created.  This is also in the onCreate of the main activity.</p>
  
  <pre><code>displayFragment = new DisplayFragment();
  </code></pre>
  
  <p>In the FragmentPagerAdapter, getItemPosition(Object item) returns 0 for the displayFragment and getItem(int index) returns the reference to displayFragment for index 0.</p>
  
  <p>This is the related code inside onConfigurationChanged.</p>
  
  <pre><code>if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
  
              this.getFragmentManager()
                      .beginTransaction()
                      .detach(displayFragment)
                      .attach(displayFragment)
                      .commitAllowingStateLoss();
              lastOrientation = Configuration.ORIENTATION_PORTRAIT;
              //this.mAdapter.notifyDataSetChanged();
  
              lastOrientation = Configuration.ORIENTATION_LANDSCAPE;
              //this.mAdapter.notifyDataSetChanged();
          }
          else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
              this.getFragmentManager()
                      .beginTransaction()
                      .detach(displayFragment)
                      .attach(displayFragment)
                      .commitAllowingStateLoss();
              lastOrientation = Configuration.ORIENTATION_PORTRAIT;
  }
  </code></pre>
  
  <p>And the stack trace.</p>
  
  <pre><code>java.lang.IllegalStateException: Fragment already added: DisplayFragment{42b66cd0 #0 id=0x7f0a0001 android:switcher:2131361793:0}
          at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1133)
          at android.app.BackStackRecord.run(BackStackRecord.java:618)
          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
          at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:479)
          at android.support.v13.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
          at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
          at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
          at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
          at android.view.View.measure(View.java:17387)
          at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
          at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
          at android.view.View.measure(View.java:17387)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
          at android.view.View.measure(View.java:17387)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
          at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
          at android.view.View.measure(View.java:17387)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
          at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
          at android.view.View.measure(View.java:17387)
          at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2211)
          at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1350)
          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1547)
          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1234)
          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6465)
          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
          at android.view.Choreographer.doCallbacks(Choreographer.java:603)
          at android.view.Choreographer.doFrame(Choreographer.java:573)
          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
          at android.os.Handler.handleCallback(Handler.java:733)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:157)
          at android.app.ActivityThread.main(ActivityThread.java:5356)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I've now tried committing each of the transactions separately, but the error still remains.</p>
  
  <pre><code>this.getFragmentManager()
      .beginTransaction()
      .detach(displayFragment)
      .commit();
  this.getFragmentManager()
      .beginTransaction()
      .attach(displayFragment)
      .commit();
  </code></pre>
  
  <p>Any pointers would be appreciated.  Thanks.</p>