If you look at the games or applications on the Google Play Store, they don't end right away when you press Back, they float toast You end the activity only when you press the dialog twice in a row. I want to make it like this I don't think there's a method that provides such a function, and I don't know what to do, so I post a question on SQ. How shall I do it?
android java back-button
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);
}
You can paste this method into the activity.
© 2024 OneMinuteCode. All rights reserved.