Search code examples
androidandroid-fragmentsandroid-architecture-navigation

Navigation component popup to fragment


I'm using the navigation component. Let's suppose you have these possible navigations

A -> C -> D -> E
B -> C -> D -> E

So you can start a sequence of screens from the two fragments A and B. Let's suppose that from E you need to go back to the fragment that started the sequence so either A or B. I can store the id of the fragment that started the sequence and then go to that using setPopUpTo when calling the action to navigate.

Is there another way to do it? Something to say pop up to C inclusive? And this should pop up C,D and E and go back to the original fragment without me specifying it. In other words I just want to keep popping up the fragments until the fragment I specify (C in this case) is popped up too.


Solution

  • To popup your stack you can use

    navController.popBackStack(R.id.action_group_to_preview, false)