Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to exit the activity in android in this case?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading