title: Android Remove Fragment and View from BackStack
body: <p><img src="https://i.stack.imgur.com/Z6clQ.png" alt="enter image description here"></p>
  
  <p>I realise this question has been asked before however the previous answers have gotten me so far. The scenario is as follows: we have a dashboard fragment (A), which leads a user to a login screen (B). On successful login they go to a listview (c). On backpress I would like to return to A, as the user will not need to see the login screen again. In addition on successful login we store the details in shared preferences and automate the login in B next time, which all works as planned.</p>
  
  <p>I have the following FragmentHelper method:</p>
  
  <pre><code>public static void goToNextFragement(Fragment fragment, int container, boolean addToBackStack, Fragment ctx)
      {
          // Create new fragment and transaction
          FragmentTransaction transaction = ctx.getSupportFragmentManager().beginTransaction();
          transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
          // Replace whatever is in the fragment_container view with this fragment,
          // and add the transaction to the back stack
          transaction.replace(container, fragment);
  
          if(addToBackStack)
              transaction.addToBackStack(null);
  
          // Commit the transaction
          transaction.commit();
      }
  </code></pre>
  
  <p>In the transaction from B to C I set the Boolean addToBackStack as false so that the <code>transaction.addToBackStack(null);</code> is not called. This again works well but after is where my problem starts.</p>
  
  <p>When the user presses back on C and returns to A I can still see the inflated view of C under the view of A.</p>
  
  <p>Any help would be appreciated. I hope my diagram helps keep this simple.</p>