Search code examples
androidexitclose-application

Android application not exiting


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.


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