I am once again asking for your intellectual support.
I have a fragment that puts a number of copies of itself on the navigation backstack like this:
Navigation.findNavController(button).navigate(
TaskCollectFragmentDirections.actionTaskCollectFragmentSelf(args.taskIndex, args.screenIndex + 1)
)
The code above works as intended.
The problem comes, when I want to pop a specified number of these fragments off the navigation backstack. (Triggered by a buttonpress.)
I've tried to do it like this:
Navigation.findNavController(button).popBackStack(lastRecurringFragmentId, false)
My code compiles and runs, but simply does nothing. When I give no parameters to the popBackStack 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.
If it is of any importance, the lastRecurringFragmentId comes from this function:
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
}
The navigation graph is as follows. There are supposed to be several TaskCollectFragments and lastly one TaskEndFragment on the top of the backstack. I want to pop some of the TaskCollectFragments off the stack when the user presses a button on the TaskEndFragment:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_nav_home_to_taskCollectFragment"
app:destination="@id/taskCollectFragment" />
</fragment>
<fragment
android:id="@+id/taskCollectFragment"
android:label="TaskCollectFragment"
tools:layout="@layout/task_collect_fragment"
>
<action
android:id="@+id/action_taskCollectFragment_to_taskEndFragment"
app:destination="@id/taskEndFragment" />
<action
android:id="@+id/action_taskCollectFragment_self"
app:destination="@id/taskCollectFragment" />
<argument
android:name="taskIndex"
app:argType="integer" />
<argument
android:name="screenIndex"
app:argType="integer" />
</fragment>
<fragment
android:id="@+id/taskEndFragment"
android:name="ui.task.TaskEndFragment"
android:label="TaskEndFragment"
tools:layout="@layout/task_end_fragment">
<argument
android:name="taskIndex"
app:argType="integer" />
</fragment>
</navigation>
Is there a way I can pop fragments off the backstack until a certain condition is met?