Question

I have an activity from where on click of back button, the app should display home page, I have written a method for exiting the page as :

private void exitQuiz() {
    Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
    startActivity(intent);
    finish();
}

In the home page again when I am pressing back button, it's not exiting the application but stays on Home activity when I press back again then only it exits the application. I further tried by adding following code to home activity to handle such scenario:

public void onBackPressed() {
    finish();
    System.exit(0);
}

but still it still exiting on one click of back button. Any help will be highly appreciated.

Was it helpful?

Solution

You should add a flag:

private void exitQuiz() {
    Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
    intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

OTHER TIPS

You're probably creating a new instance of the HomeActivity when trying to get back to it.

Check it out for a solution:

https://stackoverflow.com/a/2427385/770467

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top