title: How can I prevent "java.lang.IllegalStateException: Fragment already added" when replacing fragments?
body: <p>Despite my efforts to prevent fragments from being added more than once, I continue to encounter <code>java.lang.IllegalStateException: Fragment already added: VideoFragment</code>. </p>
  
  <p>I have an activity where VideoFragment is instantiated in onCreate only. In the only place I attempt to display the VideoFragment, I first check whether this fragment has been added already. </p>
  
  <pre><code>private VideoFragment videoFragment;
  
  public void onCreate(Bundle savedInstanceState) {
      ...
      videoFragment = new VideoFragment();
      ...
  }
  
  private void showVideoFragment() {
      if (!videoFragment.isAdded()) {
          FragmentTransaction transaction = getFragmentManager().beginTransaction();
          transaction.replace(R.id.fragment_container, videoFragment, "video").commit();
      }
  }
  </code></pre>
  
  <p>I have not been able to consistently reproduce this problem to examine in the debugger, but my runtime error reporting continues to report the exception <code>java.lang.IllegalStateException: Fragment already added: VideoFragment</code> for users, with stack traces composed of Android classes. </p>
  
  <pre><code>/FragmentManager.java:1133→ android.app.FragmentManagerImpl.addFragment
  /BackStackRecord.java:648→ android.app.BackStackRecord.run
  /FragmentManager.java:1453→ android.app.FragmentManagerImpl.execPendingActions
  /FragmentManager.java:443→ android.app.FragmentManagerImpl$1.run
  /Handler.java:733→ android.os.Handler.handleCallback
  /Handler.java:95→ android.os.Handler.dispatchMessage
  /Looper.java:146→ android.os.Looper.loop
  /ActivityThread.java:5487→ android.app.ActivityThread.main
  /Method.java:-2→ java.lang.reflect.Method.invokeNative
  /Method.java:515→ java.lang.reflect.Method.invoke
  /ZygoteInit.java:1283→ com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
  /ZygoteInit.java:1099→ com.android.internal.os.ZygoteInit.main
  /NativeStart.java:-2→ dalvik.system.NativeStart.main
  </code></pre>
  
  <p>Does the definition of added in <code>isAdded()</code> not match the one used to check fragment transactions? </p>
  
  <p>Or is there some way the videoFragment reference in the activity is not the same? Is this something I need to explicitly handle when saving state <a href="http://developer.android.com/guide/components/activities.html#SavingActivityState" rel="noreferrer">http://developer.android.com/guide/components/activities.html#SavingActivityState</a>?</p>
  
  <p>Or is there a reliable alternative way of checking whether the fragment has already been added? </p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have figured out how to semi-reliably produce the problem. </p>
  
  <ol>
  <li>Start application</li>
  <li>Navigate away from application, and run other programs for awhile. On my Galaxy Nexus (which is pretty slow these days), using Chrome to read a few news articles seems sufficient. When returning to the home screen, if it takes a few seconds to render then the application will likely throw the fragment exception. </li>
  <li>Restart application and trigger fragment change</li>
  </ol>
  
  <p>If I kill and simply run the application, everything appears to be fine. Or if I navigate away from the application and come back immediately, it works okay. It's only if the application is left in the background for a bit (enough to remove from memory?), that the fragment issue appears. </p>
  
  <p>I also tried, to no effect, in onCreate</p>
  
  <pre><code>View v = findViewById(R.id.fragment_container);
  if(v != null){
      Log.d(TAG, "disabling save for fragment_container");
      v.setSaveEnabled(false);
      v.setSaveFromParentEnabled(false);
  }
  </code></pre>
  
  <p>I also tried checking <code>Fragment prior = getFragmentManager().findFragmentByTag("video");</code> and <code>Fragment prior2 = getFragmentManager().findFragmentById(R.id.fragment_container);</code> before running the replace fragment transaction, but these come up <code>null</code>. </p>
  
  <p>My problem in fact looks very similar to 
  <a href="https://code.google.com/p/android/issues/detail?id=61247" rel="noreferrer">https://code.google.com/p/android/issues/detail?id=61247</a>
  though the time appears less an issue than memory/cache effects. It is completely unclear to me why that issue was closed. </p>
  
  <p>I will try to produce a simple application that replicates this issue. My current one uses webrtc, and the logcat output is completely cluttered with webrtc messages. </p>