Home > OS >  How to exit the activity in android in this case?
How to exit the activity in android in this case?

Time:06-20

I'm creating a quiz app with SQL and Firebase. First screens are SignUp/in activities, and after user is signed he is redirected to a splash screen which automatically navigates to PlayActivity. In play activity I wanted to handle the onBackPressedMethod, so that the user will close the game with the following code:

@Override public void onBackPressed() {

    if (backPressedTime   2000 > System.currentTimeMillis()) {
        new AlertDialog.Builder(this)
                .setTitle("Do you  want to exit?")
                .setNegativeButton("No", null)
                .setPositiveButton("Yes", (dialog, which) -> {

                    setResult(RESULT_OK, new Intent().putExtra("Exit", true));
                    System.exit(0);
                }).create().show();

    }else  {

        Toast.makeText(this, "Press Again to Exit", Toast.LENGTH_SHORT).show();
    }
    backPressedTime = System.currentTimeMillis();
}

The problem is, that after pressing the Yes button in the alert dialog, user is redirected to a previous SignUp activity, instead of exiting the game? How can I fix this?

CodePudding user response:

You need to call finishAffinity() in your SignUpActivity before you navigate to PlayActivity. This will remove the first Activity (and any below it) from the stack.

  • Related