How to remove only one fragment from backstack in fragment navigation component?

You can use navController.popBackStack(int destinationId,bool inclusive) method, (please mind boolean value)

Attempts to pop the controller's back stack back to a specific destination. eg.

navController.popBackStack(R.id.dest_id_of_B, true)

You should use

      app:popUpTo="@id/id_of_c_fragment" 
      app:popUpToInclusive="true"
      app:popUpTo="@id/id_of_d_fragment"
      app:popUpToInclusive="true"

in navigation graph in action scope.


In fragment D, if you want to back to fragment B (remove fragment C), you can use:

Navigation.findNavController(requireView()).popBackStack(
    R.id.id_of_fragment_C, true)

id_of_fragment_C is the id of fragment which you want to skip.