title: IllegalStateException when adding Fragment from Backstack
body: <p>From time to time I get the following crash report:</p>
  
  <pre><code>java.lang.IllegalStateException: 
    at androidx.fragment.app.FragmentManagerImpl.addFragment (FragmentManagerImpl.java:1916)
    at androidx.fragment.app.BackStackRecord.executePopOps (BackStackRecord.java:828)
    at androidx.fragment.app.FragmentManagerImpl.executeOps (FragmentManagerImpl.java:2622)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether (FragmentManagerImpl.java:2411)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManagerImpl.java:2366)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions (FragmentManagerImpl.java:2273)
    at androidx.fragment.app.FragmentManagerImpl$1.run (FragmentManagerImpl.java:733)
    at android.os.Handler.handleCallback (Handler.java:808)
    at android.os.Handler.dispatchMessage (Handler.java:101)
    at android.os.Looper.loop (Looper.java:166)
    at android.app.ActivityThread.main (ActivityThread.java:7529)
    at java.lang.reflect.Method.invoke (Method.java)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)
  </code></pre>
  
  <p>In line 1916 of FragmentManagerImpl I can find: </p>
  
  <pre><code>throw new IllegalStateException("Fragment already added: " + fragment);
  </code></pre>
  
  <p>So it says that some fragment is already added. Unfortunately Google does not show the message (which fragment was already added) in the Google Play Console anymore. As far as I understand the Stacktrace this exception occurs when adding a fragment from the backstack right?</p>
  
  <p>I have one <code>FrameLayout</code> in which I add/remove Fragments. I always add them with following code:</p>
  
  <pre><code>public void addFragment(FragmentActivity activity, Fragment fragment) {
      FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
      String tag = fragment.getClass().getCanonicalName();
      Fragment prev = activity.getSupportFragmentManager().findFragmentByTag(tag);
      if (fragment.isAdded()) {
          return;
      }
      if (prev != null) {
          transaction.remove(prev);
      }
      transaction.replace(R.id.fragment_container, fragment, tag);
      transaction.addToBackStack(null);
      transaction.commit();
  </code></pre>
  
  <p>And I add DialogFragments with following method:</p>
  
  <pre><code>public void openFragmentDialog(FragmentActivity activity, DialogFragment dialogFragment, String tag) {
      FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
      Fragment prev = activity.getSupportFragmentManager().findFragmentByTag(tag);
      if (prev != null) {
          transaction.remove(prev);
      }
      transaction.addToBackStack(null);
      dialogFragment.show(transaction, tag);
  }
  </code></pre>
  
  <p>In which situation could it happen that the IllegalStateException occurs? Am I adding/replacing the Fragments/DialogFragments the wrong way? I could never reproduce that error. But I get reports from Android 4.4 - Android 9 and all types of devices and I have no idea where it could happen.</p>
  
  <p>Could it be something with animations or slow devices? Because it only happens occasionally.</p>