【问题标题】:Back button will bring to home page after firebase logout on app在应用程序上的firebase注销后,后退按钮将返回主页
【发布时间】:2019-04-19 09:58:15
【问题描述】:

我的问题与这篇文章非常相似:(When I press back button on login page it will go to main menu (after I select yes for logout in MainMenu activity))。

基本上,即使在我选择“是”选项注销后,它也会让我回到缩进页面(登录页面)。但是,当我按下我的实际手机 (S7 Edge+) 和 2 个模拟器 (Nexus 4 & Pixel 2 XL) 上的返回按钮时,它再次将我带到仪表板页面,但它不应该是这样的。

**我尝试过的是我将 finish() 添加到注销功能和菜单中,如下所示:

注销功能/方法

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

菜单

@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);
}

问题仍然存在。但是在 logcat 中没有发现错误,所以我不知道如何解决这个问题?

有人有解决办法吗?请指导我。

谢谢。

【问题讨论】:

标签: java android firebase firebase-authentication logout


【解决方案1】:

所以对作者有效的实际解决方案是使用finishAffinity(),但不是公认的答案。这对我也有用。

【讨论】:

    【解决方案2】:
     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();
    }
    

    **试试这个**

    【讨论】:

    • 您好,感谢您的回复!我已经尝试过了,但我遇到了所有意图错误(无法解析符号“意图”)。我相信您的解决方案与 Jay 相似,因此我仍然会为您投票,因为它们很有用!非常感谢:>
    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2012-05-12
    • 2020-06-04
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多