There are 3 activities in my app.
Activity A(Main Page) -> Activity B -> Activity C.
When submit button
in Activity C is clicked, it will back to Activity A again. When I click the back button in device to exit the app, it will back to Activity C again, then B and A , then only can exit.
Is there a way to let the app straight away exit when back button in
device is pressed in Activity A?
I add below code in Activity A, but the problem is it still goes to Activity C instead of exit.
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…