0

My issue is very similar to this post: (When I press back button on login page it will go to main menu (after I select yes for logout in MainMenu activity)).

Basically, even after I select "YES" option to logout, it does bring me back to the indented page (Login page). However, when I press the back button on my actual phone (S7 Edge+) and 2 emulators (Nexus 4 & Pixel 2 XL), it bring me to the dashboard page again which it should not be that way.

**WHAT I HAVE TRIED was I added finish() to Logout function and in the menu as seen below:

Logout function/method

private void Logout(){
    firebaseAuth.signOut();
    finish();
    startActivity(new Intent(SecondActivity.this, MainActivity.class));
    finish();
}

Menu

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.logoutMenu: {
            final AlertDialog.Builder builder = new AlertDialog.Builder(SecondActivity.this);
            builder.setMessage("Are you sure you want to logout?");
            builder.setCancelable(true);
            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent(SecondActivity.this, SecondActivity.class));
                }
            });
            builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(SecondActivity.this, "You are succesfully signed out!", Toast.LENGTH_LONG).show();
                    Logout();
                    finish();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            break;
        }
        case R.id.profileMenu: {
            startActivity(new Intent(SecondActivity.this, UpdateProfileActivity.class));
            break;
        }
        case R.id.passwordMenu: {
            startActivity(new Intent(SecondActivity.this, UpdatePasswordActivity.class));
            break;
        }
    }
    return super.onOptionsItemSelected(item);
}

The issue still persist. There is no error found in logcat though so I am not sure how to overcome this issue?

Does anyone have a solution? Please do guide me.

Thank you.

  • 1
    When you press logout just add clear top flag and call `finishAffinity()` instead of `finish()` which will finish your all previous activities. – Piyush Nov 16 '18 at 08:30
  • Try using [Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP](https://stackoverflow.com/a/11472684/5990846) – Jay Nov 16 '18 at 08:32
  • @Piyush finishAffinity() did it! Thank you very much! –  Nov 16 '18 at 08:50
  • @Jay Very useful! Thank you! –  Nov 16 '18 at 08:51
  • Check also **[this](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth)** out. – Alex Mamo Nov 16 '18 at 13:12

2 Answers2

1

So the actual solution worked for the author is using finishAffinity() but not the accepted answer. Which worked for me too.

0
 private void Logout(){
    firebaseAuth.signOut();
    Intent intent = new Intent(SecondActivity.this, MainActivity.class);
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
    finish();
}

**Just try this **

Anand
  • 1,866
  • 3
  • 26
  • 49
  • Hi thanks for your response! I have tried and I'm having all the intent error (Cannot resolve symbol 'intent'). I believe your solution is similar to Jay and so I will still give you an upvote as they are useful! Many thanks :> –  Nov 16 '18 at 08:49