title: How to clear navigation Stack after navigating to another fragment in Android
body: <p>I am using The new <strong><a href="https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing" rel="noreferrer">Navigation Architecture Component</a></strong> in android and I am stuck in clearing the navigation stack after moving to a new fragment. </p>
  
  <p>Example: 
  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.</p>
  
  <p>I am using a simple <a href="https://developer.android.com/reference/androidx/navigation/fragment/NavHostFragment#findNavController(android.support.v4.app.Fragment)" rel="noreferrer">NavHostFragment.findNavController(Fragment).navigate(R.id.homeFragment)</a> to navigate.</p>
  
  <p>Current Code : </p>
  
  <pre><code>mAuth.signInWithCredential(credential)
              .addOnCompleteListener(getActivity(), new OnCompleteListener&lt;AuthResult&gt;() {
                  @Override
                  public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                      if (task.isSuccessful()) {
                          NavHostFragment.findNavController(LoginFragment.this).navigate(R.id.homeFragment);
                      } else {
                          Log.w(TAG, "signInWithCredential:failure", task.getException());
                      }
                  }
              });
  </code></pre>
  
  <p>I tried using the <a href="https://developer.android.com/reference/androidx/navigation/NavOptions.Builder" rel="noreferrer">NavOptions</a> in the <a href="https://developer.android.com/reference/androidx/navigation/NavController#navigate_7" rel="noreferrer">navigate()</a>, but the back button is still sending me back to the loginFragment</p>
  
  <pre><code>NavOptions.Builder navBuilder = new NavOptions.Builder();
  NavOptions navOptions = navBuilder.setPopUpTo(R.id.homeFragment, false).build();   
               NavHostFragment.findNavController(LoginFragment.this).navigate(R.id.homeFragment, null, navOptions);
  </code></pre>