title: Android Fragments overlapping when popping back
body: <p>I have a Fragment container which gets the current Fragment shown. When pressing the back button I want to remove the topmost Fragment but when it's the first one, I want to move the App in the background instead. (Otherwise, my main fragment would get removed, too.)</p>
  
  <p>So my (Kotlin) code looks like that:</p>
  
  <pre><code>override fun onBackPressed() {
      ...
      when (supportFragmentManager.backStackEntryCount) {
          1    -&gt; {
              moveTaskToBack(true)
          }
          else -&gt; {
             super.onBackPressed()
             //supportFragmentManager.popBackStackImmediate()
          }
      }
  </code></pre>
  
  <p>This works fine with one exception. When I start the app and show the second Fragment I press the back button twice (rapidly). That way the top Fragment starts to get removed but the second back press kicks in directly afterward (with the top Fragment not removed completely).
  The app goes into the background (as intended) but when I bring the app into the foreground again, the top Fragment is still there (sometimes partially removed already) but it does not react anymore. If I replace another fragment in my container, it appears behind the stuck Fragment.
  I also tried using supportFragmentManager.popBackStackImmediate() instead but it also didn't work.</p>
  
  <p><strong>So I have two questions here:</strong></p>
  
  <ul>
  <li><p>how can I continue the cleanup afterward (when the App becomes visible again)</p></li>
  <li><p>or if that's not the solution, how can I check if there is still a Fragment transaction ongoing in the FragementManager (before putting the app into the background with the second back press)</p></li>
  </ul>
  
  <p>---- Edit 1 ----</p>
  
  <p>I noticed this only happens when I set transaction animation when replacing the fragments. So if I remove that line the moment I add the Fragments (not when back pressing) it seems to work:</p>
  
  <pre><code>  ft.setCustomAnimations(R.anim.enter_from_bottom, R.anim.exit_to_top,
     R.anim.enter_from_top, R.anim.exit_to_bottom)
  </code></pre>
  
  <p>---- Edit 2 ----</p>
  
  <p>When using this code here it also works (just with the difference that it removes the first (bottom) fragment, too. But obviously the native Android method seems to avoid that problem.</p>
  
  <pre><code>super.onBackPressed() // Remove bottom fragement
  super.onBackPressed() // No more fragments -&gt; move App into background
  </code></pre>
  
  <p>---- This is how the Fragments are "created" ----</p>
  
  <pre><code>val ft = supportFragmentManager.beginTransaction()
  ft.setCustomAnimations(R.anim.enter_from_bottom, R.anim.exit_to_top, 
      R.anim.enter_from_top, R.anim.exit_to_bottom)
  ft.replace(R.id.fragmentContainer, fragment, fragment::class.java.simpleName)
      .addToBackStack(fragment::class.java.simpleName)
      .commitAllowingStateLoss()
  </code></pre>
  
  <p>Here is a screenshot of how the overlapping Fragments are looking:</p>
  
  <p><a href="https://i.stack.imgur.com/Zk1XX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zk1XX.png" alt="Screenshot of the overlapping Fragments"></a></p>