title: How to finish a fragment?
body: <p>I'm doing an APP with a login system, and now I have a problem when I'm logged, when I enter the credentials, I replace the fragment with another interface, but If user press the back button, the app come back to the login screen (fragment). I want to know how to remove the login screen if the user has logged in.</p>
  
  <p>I change the fragments with:</p>
  
  <pre><code>FragmentTransaction transaction_to_main = getFragmentManager().beginTransaction();
                  transaction_to_main.setCustomAnimations(R.anim.modal_in, R.anim.modal_out);
                  transaction_to_main.replace(R.id.container, new MainFragment());
                  transaction_to_main.addToBackStack(null);
                  transaction_to_main.commit();
  </code></pre>
  
  <p>I'm trying to use this part of code that I have read in the forum to clear the backstack, but I get a NullPointerException</p>
  
  <pre><code>while (getFragmentManager().getBackStackEntryCount() &gt; 0){
                      getFragmentManager().popBackStackImmediate();
                  }
  </code></pre>
  
  <p>Thank you</p>
  
  <p>EDIT:</p>
  
  <p>In a main activity, in onCreate:</p>
  
  <pre><code>if(savedInstanceState != null || shared_pref.contains("access_token")){
              shared_pref_token = shared_pref.getString("access_token", "");
              shared_pref_refresh_token = shared_pref.getString("refresh_token", "");
  
              //Check if token is valid, if not, a new token is requested
              check_token(shared_pref_token);
  
          }else{
              iv_logout.setVisibility(View.GONE);
              FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
              transaction.setCustomAnimations(R.anim.modal_in, R.anim.modal_out);
              transaction.replace(R.id.container, new AccessFragment());
              transaction.addToBackStack(null);
              transaction.commit();
              //getSupportFragmentManager().beginTransaction().setTransition(R.anim.modal_in).replace(R.id.container, new AccessFragment()).commit();
          }
  </code></pre>
  
  <p>The AccessFragment has in the onCreate:</p>
  
  <pre><code>bt_login.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  FragmentTransaction transaction = getFragmentManager().beginTransaction();
                  transaction.setCustomAnimations(R.anim.modal_in, R.anim.modal_out);
                  transaction.replace(R.id.container, new LoginFragment());
                  transaction.addToBackStack(null);
                  transaction.commit();
                  getFragmentManager().beginTransaction().remove(getTargetFragment()).commit();
                  /*new SweetAlertDialog(getActivity(), SweetAlertDialog.SUCCESS_TYPE)
                          .setTitleText("Sesión iniciada en GECAS")
                          .show();*/
              }
          });
  
          bt_register.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  FragmentTransaction transaction = getFragmentManager().beginTransaction();
                  transaction.setCustomAnimations(R.anim.modal_in, R.anim.modal_out);
                  transaction.replace(R.id.container, new RegisterFragment());
                  transaction.addToBackStack(null);
                  transaction.commit();
                  getFragmentManager().beginTransaction().remove(getTargetFragment()).commit();
              }
          });
  </code></pre>