Back button will bring to home page after firebase logout on app

二次信任 提交于 2020-12-15 06:52:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!