How to close a fragment manually in android NavigationComponent

Viewed 5235

I'm switched to NavigationComponent and i don't know how close fragments manually using this component, in activities we have finish() method that close an activity, i need to do similar thing like this in NavigationComponent.

4 Answers

Kotlin

I do it in this way

findNavController().popBackStack()

you can use below line in Fragment class:

getActivity().getParentFragmentManager().popBackStack();

or if you are in an activity then you can use

getParentFragmentManager().popBackStack();

If you're using the Navigation component, you can also simply call Navigator.popBackStack() where Navigator is your Navigator object.

Here's the documentation for the NavController

The best way to do this:

requireActivity().onBackPressed()

Related