title: How do I pop android navigation backstack up to a certain fragment?
body: <p>I am once again asking for your intellectual support.</p>
  <p>I have a fragment that puts a number of copies of itself on the navigation backstack like this:</p>
  <pre><code>Navigation.findNavController(button).navigate(
      TaskCollectFragmentDirections.actionTaskCollectFragmentSelf(args.taskIndex, args.screenIndex + 1)
  )
  </code></pre>
  <p>The code above works as intended.</p>
  <p>The problem comes, when I want to pop a specified number of these fragments off the navigation backstack. (Triggered by a buttonpress.)</p>
  <p>I've tried to do it like this:</p>
  <pre><code>Navigation.findNavController(button).popBackStack(lastRecurringFragmentId, false)
  </code></pre>
  <p>My code compiles and runs, but simply does nothing. When I give no parameters to the <code>popBackStack</code> function, it properly pops off the topmost fragment. I can also use the back arrow to navigate back all the way to the starting fragment.</p>
  <p>If it is of any importance, the <code>lastRecurringFragmentId</code> comes from this function:</p>
  <pre><code>fun getLastRecurringFragmentId(recurringFragmentCount: Int): Int {
      val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)!!
      val backStackEntryCount = navHostFragment.childFragmentManager.backStackEntryCount
      val indexOfRequested = backStackEntryCount - recurringFragmentCount
      val requestedEntry = navHostFragment.childFragmentManager.getBackStackEntryAt(indexOfRequested)
      return requestedEntry.id
  }
  </code></pre>
  <p>The navigation graph is as follows. There are supposed to be several <code>TaskCollectFragment</code>s and lastly one <code>TaskEndFragment</code> on the top of the backstack. I want to pop some of the <code>TaskCollectFragment</code>s off the stack when the user presses a button on the <code>TaskEndFragment</code>:</p>
  <pre><code>&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:id=&quot;@+id/mobile_navigation&quot;
      app:startDestination=&quot;@id/nav_home&quot;&gt;
  
      &lt;fragment
          android:id=&quot;@+id/nav_home&quot;
          android:name=&quot;ui.home.HomeFragment&quot;
          android:label=&quot;@string/menu_home&quot;
          tools:layout=&quot;@layout/fragment_home&quot; &gt;
          &lt;action
              android:id=&quot;@+id/action_nav_home_to_taskCollectFragment&quot;
              app:destination=&quot;@id/taskCollectFragment&quot; /&gt;
      &lt;/fragment&gt;
  
      &lt;fragment
          android:id=&quot;@+id/taskCollectFragment&quot;
          android:label=&quot;TaskCollectFragment&quot;
          tools:layout=&quot;@layout/task_collect_fragment&quot;
          &gt;
          &lt;action
              android:id=&quot;@+id/action_taskCollectFragment_to_taskEndFragment&quot;
              app:destination=&quot;@id/taskEndFragment&quot; /&gt;
          &lt;action
              android:id=&quot;@+id/action_taskCollectFragment_self&quot;
              app:destination=&quot;@id/taskCollectFragment&quot; /&gt;
          &lt;argument
              android:name=&quot;taskIndex&quot;
              app:argType=&quot;integer&quot; /&gt;
          &lt;argument
              android:name=&quot;screenIndex&quot;
              app:argType=&quot;integer&quot; /&gt;
      &lt;/fragment&gt;
      &lt;fragment
          android:id=&quot;@+id/taskEndFragment&quot;
          android:name=&quot;ui.task.TaskEndFragment&quot;
          android:label=&quot;TaskEndFragment&quot;
          tools:layout=&quot;@layout/task_end_fragment&quot;&gt;
          &lt;argument
              android:name=&quot;taskIndex&quot;
              app:argType=&quot;integer&quot; /&gt;
      &lt;/fragment&gt;
  &lt;/navigation&gt;
  </code></pre>
  <p>Is there a way I can pop fragments off the backstack until a certain condition is met?</p>