title: Why does my fragment is still added to the backstack?
body: <p>I'm trying to migrate a part of my app to single activity, and I have 2 kind of fragments: those that I want the user to be able to go back to, and those that I are just &quot;temporary&quot; screens that I don't want to user to be able to go back to.</p>
  <p>I thought I could use the backstack for that but it doesn't work.</p>
  <p>For the first ones I use:</p>
  <pre class="lang-kotlin prettyprint-override"><code>supportFragmentManager.commit {
      replace(binding.fragmentContainer.id, MyFragment(), MyFragment.TAG)
      addToBackStack(MyFragment.TAG)
  }
  </code></pre>
  <p>And for the second ones (&quot;temporary&quot; screens) I use:</p>
  <pre class="lang-kotlin prettyprint-override"><code>supportFragmentManager.commit {
      replace(binding.fragmentContainer.id, MyFragment())
  }
  </code></pre>
  <p>When I check <code>supportFragmentManager.mBackStack</code> I can see the backstack is setup as intended, but it doesn't seems to matter, when <code>onBackPressed</code> is called, the user can go back to fragments that are not on the backstack.</p>
  <p>So I tried to override <code>onBackPressed</code> like this:</p>
  <pre class="lang-kotlin prettyprint-override"><code>override fun onBackPressed() {
      if (supportFragmentManager.backStackEntryCount &gt; 1)
          supportFragmentManager.popBackStack()
  }
  </code></pre>
  <p>But it doesn't seems to work either.</p>
  <p>EDIT:
  To be clear, I have a <code>FragmentContainerView</code> named <code>fragmentContainer</code> within my Activity and add the very first fragment using the first code sample I provided, then I add a second fragment using second code sample, then a third fragment using the first code sample. My backstack then look like this:</p>
  <ul>
  <li>MyFirstFragment</li>
  <li>MyThirdFragment</li>
  </ul>
  <p>&quot;MySecondFragment&quot; wasn't added to the backstack, as intended, it look exactly like I wanted to, but when I do a <code>popBackstack</code>, I don't go back to MyFirstFragment but to MySecondFragment, for unknown reason.</p>