Home > Software engineering >  Navigation component popup to fragment
Navigation component popup to fragment

Time:09-28

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.

CodePudding user response:

to avoid rememer destination in backstack you can do in this way(kotlin):

   val firstFragment = findNavController().backQueue.firstOrNull { it.destination.id == R.id.fragmentA }?.destination?.id
   firstFragment?.let {
      val navOptions = NavOptions.Builder()
      navOptions.setPopUpTo(firstFragment,false)
      findNavController().navigate(firstFragment,null,navOptions.build())
    }

CodePudding user response:

To popup your stack you can use

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