How to clear navigation Stack after navigating to another fragment in Android

The question:

I am using The new Navigation Architecture Component in android and I am stuck in clearing the navigation stack after moving to a new fragment.

I am in the loginFragment and I want this fragment to be cleared from the stack when I navigate to the home fragment so that the user will not be returned back to the loginFragment when he presses the back button.

I am using a simple NavHostFragment.findNavController(Fragment).navigate( to navigate.

Current Code :

            .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                    } else {
                        Log.w(TAG, "signInWithCredential:failure", task.getException());

I tried using the NavOptions in the navigate(), but the back button is still sending me back to the loginFragment

NavOptions.Builder navBuilder = new NavOptions.Builder();
NavOptions navOptions = navBuilder.setPopUpTo(, false).build();   
             NavHostFragment.findNavController(LoginFragment.this).navigate(, null, navOptions);

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

First, add attributes app:popUpTo='your_nav_graph_id' and app:popUpToInclusive="true" to the action tag.

    tools:layout="@layout/fragment_sign_in" >
        app:popUpToInclusive="true" />

Second, navigate to the destination, using above action as parameter.


See the docs for more information.

NOTE: If you navigate using method navigate(@IdRes int resId), you won’t get the desired result. Hence, I used method navigate(@NonNull NavDirections directions).

Method 2

I think your question specifically pertains on how to use the Pop Behavior / Pop To / app:popUpTo (in xml)

In documentation,
Pop up to a given destination before navigating. This pops all non-matching destinations from the back stack until this destination is found.

Example (Simple Job hunting app)
my start_screen_nav graph is like this:

startScreenFragment (start) -> loginFragment -> EmployerMainFragment

                            -> loginFragment -> JobSeekerMainFragment

if I want to navigate to EmployerMainFragment and pop all including startScreenFragment then the code will be:


            app:popUpToInclusive="true" />

if I want to navigate to EmployerMainFragment and pop all excluding startScreenFragment then the code will be:



if I want to navigate to EmployerMainFragment and pop loginFragment but not startScreenFragment then the code will be:






Method 3

In my case i needed to remove everything in the back Stack before i open a new fragment so i used this code

navController.popBackStack(, true);

the first line removes the back Stack till it reaches the fragment specified in my case it’s the home fragment so it’s removes all the back stack completely , and when the user clicks back in the fragment_company he closes the app.

Method 4

NOTE: Clear task is deprecated, official description is

This method is deprecated. Use setPopUpTo(int, boolean) with the id of the NavController’s graph and set inclusive to true.

Old Answer

If you don’t wanna go through all that fuzz in code, you can simply check Clear Task in Launch Options in properties of the action.

Launch Options

Edit: As of Android Studio 3.2 Beta 5, Clear Task is no longer visible in Launch Options window, but you can still use it in navigation’s XML code, in action tag, by adding


Method 5

    NavController navController 
    =Navigation.findNavController(requireActivity(),     ;// initialize navcontroller

    if (navController.getCurrentDestination().getId() == //for avoid crash
    NavDirections action = 

    //for clear current fragment from stack
    NavOptions options = new 
    NavOptions.Builder().setPopUpTo(, true).build();
    navController.navigate(action, options);

Method 6

I finally figure it out thanks to How to disable UP in Navigation for some fragment with the new Navigation Architecture Component?

I had to specify .setClearTask(true) as a NavOption.

            .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithCredential:success");

                        NavOptions.Builder navBuilder = new NavOptions.Builder();
                        NavOptions navOptions = navBuilder.setClearTask(true).build();
                    } else {
                        Log.w(TAG, "signInWithCredential:failure", task.getException());



Method 7

use this code


then call new Fragment
android version 4.1.2

Method 8

Going to add another answer here as none of the above worked for me … we have multiple nav graphs.

findNavController().navigate(,null,NavOptions.Builder().setPopUpTo(findNavController().graph.startDestination, true).build())

This was the only way that I could successfully clear the full back stack. Google really need to make this simpler.

Method 9

Here is how I am getting it done.

 //here the refer to the fragment one wants to pop back once pressed back from the newly  navigated fragment
 val navOption = NavOptions.Builder().setPopUpTo(, false).build()

//now how to navigate to new fragment
                    .navigate(, null, navOption)

Method 10

You can override the back pressed of the base activity like this :

override fun onBackPressed() {

  val navigationController = nav_host_fragment.findNavController()
  if (navigationController.currentDestination?.id == {
  } else if (navigationController.currentDestination?.id == {
    // do nothing
  } else {


Method 11

In my case where I used Navigation component with NavigationView (menu drawer):


mNavController.popBackStack(, true)
                .setPopUpTo(, true)

I wanted to clear the stack after clicking on logout on side menu drawer!
Hope that helped someone!

Method 12


// Navigation library
    def nav_version = "2.3.5"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

This solution work for me

findNavController().popBackStack(<Current Fragment Id In NavGraph>, true)

findNavController().navigate(< Your Destination Fragment  in NavGraph>)

Method 13

Non of the solutions above works for me.
After spending hours on it, here is my solution:

Note: I have multiple nav_graphs and switching between fragments in different nav_graphs.

  1. Define your action as below in xml:
    app:popUpToInclusive="true" />
  1. Navigate using action above from your Java/Kotlin code:
findNavController( {

Method 14

I struggled for a while to prevent the back button from going back to my start fragment, which in my case was an intro message that should only appear once.

The easy solution was to create a global action pointing to the destination that the user should stay on. You have to set app:popUpTo="..." correctly – set it to the destination you want to get popped off. In my case it was my intro message. Also set app:popUpToInclusive="true"

Method 15

You can do as simple as:


If you want to check the count you can do as:


All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment