I have been encountering problems with the navigation component not popping the stack. This seemingly worked as expected until recently, the navigation-fragment and navigation-ui dependencies have not changed. It was originally 2.5.3 and attempted updating to 2.7.6 without resolve.
ext.nav_version = '2.5.3'
// Navigation Component
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
Here is how its been done in the nav graph
<fragment
android:id="@+id/fragmentA"
android:name="com.example.views.AFragment"
android:label="@string/a_fragment_title">
<action
android:id="@+id/action_fragmentA_to_fragmentB"
app:destination="@id/fragmentB"
app:popUpTo="@id/fragmentA"
app:popUpToInclusive="true"/>
</fragment>
Then its called like this
fun NavController.safeNavigate(@IdRes resId: Int, args: Bundle? = null) {
currentDestination?.getAction(resId)?.run { navigate(resId, args) }
}
fun NavController.safeNavigate(@IdRes resId: Int, args: Bundle? = null, navOptions: NavOptions?) {
currentDestination?.getAction(resId)?.run { navigate(resId, args, navOptions) }
}
// Normal way I would use this
findNavController().safeNavigate(R.id.action_fragmentA_to_fragmentB, bundleOf(
"keyA" to valueA,
"keyB" to valueB
))
// Attempted to manually remove the stack but also doesnt work.
findNavController().safeNavigate(R.id.action_fragmentA_to_fragmentB, bundleOf(
"keyA" to valueA,
"keyB" to valueB
), NavOptions.Builder().setPopUpTo(R.id.fragmentA, true).build())
What the stack would look like
launcher - start
login <- move to dashboard, pop stack and set dashboard as the new start
dashboard - new start
fragmentA <- Should pop the stack moving to fragmentB
fragmentB <- Using the back button returns to fragmentA and should return to dashboard (fragmentA should NOT be on the stack)