java – Preserve Activity after pressing the "Back" button


I am working on an application with which performs an authentication through an actividad "A" ( LoginActivity ), this instance the actividad "B" ( LoginTask ) that inherits from AsyncTask to carry out this process, once the authentication is performed it is instantiated to the actividad "C" ( PrincipalActivity ), being something like this A -> B -> C.

Located in actividad "C" , I press the "Back" button and it "takes" me out of the main activity and places me in actividad "A" , that is, it returns me to the login screen. Here my code:


public class LoginActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {

    private void initUI() {
        Typeface font = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/NotoSans-Regular.ttf");

        final EditText txtUser = (EditText)findViewById(;
        final EditText txtPass = (EditText)findViewById(;
        Button btnAccess = (Button) findViewById(;

        btnAccess.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Login loginBean = new Login();
                String user = txtUser.getText().toString().trim();
                String pass = txtPass.getText().toString().trim();

                if (!user.equals("")) {
                    if(!pass.equals("")) {
                    } else {
                } else {

                LoginTask loginTask = new LoginTask(LoginActivity.this,loginBean);


public class LoginTask extends AsyncTask {
    private ProgressDialog progressDialog;
    private Context context;
    private Resources resources;
    private Login loginParams;
    private String msgAccess;

    public LoginTask(Context context, Login loginParams) {
        this.context = context;
        this.resources = context.getResources();
        this.loginParams = loginParams;

    protected void onPreExecute() {
        progressDialog = new ProgressDialog(context);

    protected String doInBackground(Object[] objects) {
        return msgAccess;

    protected void onPostExecute(Object o) {
        if (msgAccess != null) {
            if (msgAccess.equals("OK")) {
               Intent intent = new Intent(context, PrincipalActivity.class);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            } else {
                Toast.makeText(context, msgAccess, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, R.string.err_msg_null, Toast.LENGTH_LONG).show();

        if (progressDialog.isShowing()) {

So, what I had thought was to perform some kind of validation in the onBackPressed() event of my main class so that when I press the "Back" button, it does not return to "A", but rather, it keeps me in the main activity "C" :

public void onBackPressed() {
    if (LoginPersistence.getStatus() > 0 && !LoginPersistence.getToken().equals("")) {
 //Agregar aqui la validación

To be more explicit, what I indicate is that when I am in my main activity "C" and I press the "Back" button, it returns me to activity "A", that is, to enter user and password again, when it should not do it. Pressing the "Back" button should "minimize" my app and when I log back in it should keep it in the main activity. This is what I do when I press the "Home" button, it minimizes the application and when I return, it loads me where I left off and I do this with intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); .

I hope I have explained myself. Thank you.


Based on your question, you want to go back to "activity B", LoginTask but remember that this is not actually an Actividad is an AsyncTask .

  • "A" (LoginActivity)
  • "B" (LoginTask)
  • "C" (PrincipalActivity)

LoginTask is a Thread that actually performs an Intent to open the PrincipalActivity activity.

 Intent intent = new Intent(context, PrincipalActivity.class);

If you want to return to "B" LoginTask , then this class must extend Activity and within this class an instance the Asynctask with the same functionality.

Scroll to Top