The phone back button has me baffled. This question has been asked many times, but i have NOT got the solution. So asking again as per my requirement.
I have a home screen activity
It calls 3 activities – which in turn calls 2 activity each.
Assume i navigate this way
Home > Act1 > SubAct1a > SubAct1b > Home (through a Home button) > Act2 > Home > Act3
Now in Act3, when i press the phone BACK button, i get to Home. My REQUIREMENT at this point is when i press the BACK button again , it should EXIT the app. But what happens it it goes like this
Act2 > Home > SubAct1b > SubAct1a > Act1 > Home > EXIT
So the user is pressing BACK MULTIPLE times before he quits the app.
Pressing the HOME key is an option, but i want the BACK to work correctly as well.
I have this piece of code in the HOME activity – but it does not work at all
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
It just returns to the previous activity it was at.
You should return true, to tell the system that you already handled this situation,
When you start your home activity, try setting the clear top flag…