title: Clear Fragment Backstack except top fragment
body: <p>In my app I have some menu items in a Navigation Drawer. When the user taps one of these items and navigates to the corresponding fragment, I want the fragment backstack to be cleared EXCEPT for the fragment they just navigated to. Let me illustrate:</p>
  
  <p>In my nav drawer the user has options to open fragments F1, F2, F3, F4, F5, F6. When the app is first opened, the user starts on F1. Let's say from F1, the user opens fragments F7 -> F8 -> F9 -> F10 using buttons within the fragment views themselves. At this point, the backstack looks like:</p>
  
  <p>F1 -> F7 -> F8 -> F9 -> F10</p>
  
  <p>Suppose from here, the user opens the nav drawer and selects F2. What I want to happen is that the above backstack is cleared and is populated with ONLY F2, so that they user can't go back to F10 from F2. In otherwords, the backstack should become:</p>
  
  <p>Not this: F1 -> F7 -> F8 -> F9 -> F10 -> F2<br>
  But this: F2</p>
  
  <p>How can I accomplish this?</p>
  
  <p>EDIT 1:
  One of my attempted solutions was to add an <code>OnNavigationItemSelectedListener</code> to the navigationView when the activity is loading that would clear the backstack when one of the items was pressed. My solution worked, but in the process I overrode the default code that actually changes to the new fragment. I snooped through the Android API and found this:</p>
  
  <pre class="lang-java prettyprint-override"><code>navigationView.setNavigationItemSelectedListener(
      new NavigationView.OnNavigationItemSelectedListener() {
      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
          boolean handled = onNavDestinationSelected(item, navController);
          if (handled) {
              ViewParent parent = navigationView.getParent();
              if (parent instanceof DrawerLayout) {
                  ((DrawerLayout) parent).closeDrawer(navigationView);
              } else {
                  BottomSheetBehavior bottomSheetBehavior =
                                      findBottomSheetBehavior(navigationView);
                  if (bottomSheetBehavior != null) {
                       bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                  }
              }
          }
          return handled;
      }
  });
  </code></pre>
  
  <p>This is NOT my code. This is the native code that handles selection of menu items on the navigation drawer (found in the <code>NavigationUI</code> class). If I want to add my own <code>OnNavigationItemSelectedListener</code> I would have to recreate the functionality shown above, which I can't do since some of the methods in there are protected. So that's why I don't believe this solution will work.</p>