1

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.

Sid
  • 582
  • 3
  • 7
  • 28

2 Answers2

1

You should add a flag:

private void exitQuiz() {
    Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
    intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}
EvilDuck
  • 4,386
  • 23
  • 32
0

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

Community
  • 1
  • 1
Androiderson
  • 16,865
  • 6
  • 62
  • 72
  • thanks but I can't use that because there is an intermediate page between my home page and the current activity, so it will go to intermediate activity rather than home activity. – Sid Sep 14 '13 at 17:57
  • Thanks you are right I was creating a new instance. – Sid Sep 14 '13 at 18:17