简体   繁体   English

应用程序返回按两次退出-不能正常工作

[英]App Back Press Twice to Log Out - Not Working Properly

I have a bit of a weird one that I can't quite figure out. 我有些奇怪,我不太明白。

When I log out of my app via the navigation menu, it takes me back to the login screen. 当我通过导航菜单退出应用程序时,它将带我返回登录屏幕。 I have then set it up so that when the user presses the back button twice, it should close the app completely. 然后,我对其进行了设置,以便当用户按下后退按钮两次时,它应该完全关闭该应用程序。 I have got the toast to appear after one press to say the user needs to press twice but here's my issue. 一按后我就吐司了,说用户需要按两次,但这是我的问题。

When I press back twice, the screen clears but the login screen pops back up again. 当我按两次后退时,屏幕将清除,但登录屏幕会再次弹出。 With my last login details in the boxes. 在框中输入我的上次登录详细信息。 Then if I press back twice again, it closes the app completely. 然后,如果我再按两次,它将完全关闭该应用程序。 I need to try and figure out why it won't close the app on the first two presses of the back button. 我需要尝试弄清楚为什么它在返回按钮的前两次按下后无法关闭应用程序。

Below is the code I am using: 以下是我正在使用的代码:

public void onBackPressed() {
    //moveTaskToBack(true);
    if (!isUserClickedBackButton){
        Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
        isUserClickedBackButton = true;
    } else {
        System.exit(0); // exits right out of app
        super.onBackPressed();

    }
}

I have tried not using 'super.onBackPressed', I've tried using it on its own. 我尝试过不使用“ super.onBackPressed”,而是尝试过单独使用它。 I've tried adding 'finish()' or just using that on its own. 我尝试添加'finish()'或仅单独使用它。 I'm at a loss. 我很茫然。 Has anyone got any ideas? 有人知道吗?

I'm using Firebase for authentication if that makes any difference. 如果这有任何区别,我正在使用Firebase进行身份验证。

Thanks in advance. 提前致谢。

You can use: 您可以使用:

super.onBackPressed();

And add in the AndroidManifest.xml to the login activity tag: 并将AndroidManifest.xml添加到登录活动标签中:

android:noHistory="true"

You Can do it without using no History in android manifest if you want... 您可以在不使用Android清单中不使用任何历史记录的情况下执行此操作...

//Use this as class variable..
boolean doublePressedBackExit = false;   


@Override
public void onBackPressed() {
    if (doublePressedBackExit) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        System.exit(0);
        return;
    }
    this.doublePressedBackExit = true;
    Toast.makeText(getApplicationContext(), "Press again to exit..", Toast.LENGTH_SHORT).show();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doublePressedBackExit = false;
        }
    }, 2000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM