How can I pop backstack until one of the ids found or see the whole backstack?

13 views Asked by At

I'm trying to navigate back from a Fragment using jetpack navigation, but my problem is that there are 2 different ways to get there. So, I'm on Fragment C, and I can get there with

A -> ... -> C

B -> ... -> C

but it is also possible to go like this:

A -> B -> A -> ... -> C

How can I know where should I pop back to?

I'm using this extension method (found on SO), but this only gives back that the Fragment exist on the backstack or not, which is not enough for me (see the third case):

private fun NavController.isOnBackStack(@IdRes id: Int): Boolean = try { getBackStackEntry(id); true } catch (e: Throwable) { false }

I also tried all the Fragment.parentFragmentManager's methods, but the BackStackEntry doesn't hold the Fragment's name or id.

0

There are 0 answers