title: How to remove the first fragment added to the FragmentManager and start a fresh BackStack?
body: <p>So I decided to use the Fragment backstack in my Single Activity, Multiple Fragments pet project, and I'm running into problems right from the start.</p>
  
  <p>My app flow is like this -> Activity starts, then does this:</p>
  
  <pre><code>supportFragmentManager.beginTransaction()
          .add(R.id.container, OnboardingFragment.newInstance())
          .commit()
  </code></pre>
  
  <p>This first fragment is OnboardingFragment, then the user navigates forward to SignInFragment, where I have this method in my Activity:</p>
  
  <pre><code>fun replaceFragment(fragment: Fragment) {
      supportFragmentManager.beginTransaction()
          .replace(R.id.container, fragment)
          .addToBackStack(null)
          .commit()
  }
  </code></pre>
  
  <p>I use this method to start <code>SignInFragment()</code>, and I continue to use this method for the navigating the app afterwards.</p>
  
  <p>However, what I'd like to do is after the user signs in, I'd like to completely remove all the backstack entries and Fragments, and start with a clean slate.</p>
  
  <p>The problem with that is if do <code>popBackStack</code> until <code>backStackEntryCount == 0</code>, the <code>OnboardingFragment</code> is never popped off the stack, because it wasn't added to it in the first place. Adding it to backstack at the beginning introduced a bug where the backstack is cleaned, but you'd have an empty view if you press Back or Up.</p>
  
  <p>I tried doing <code>remove(OnboardingFragment())</code>, but that didn't work either, and doing <code>add(R.id.container, Fragment())</code> on top of it doesn't work either because its UI remains underneath the new UI. even hide() didn't work. Why does that happen anyway?</p>
  
  <p>TLDR; how can I remove the first fragment added to the container, if it's not on the back stack?</p>