问题
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:
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 **
回答2:
So the actual solution worked for the author is using finishAffinity()
but not the accepted answer. Which worked for me too.
来源:https://stackoverflow.com/questions/53334017/back-button-will-bring-to-home-page-after-firebase-logout-on-app