Clicking the back button twice to exit an activity
I have noticed this pattern in a lots of android apps and games recently. When clicking the hardware back button to exit the application , a toast message comes up with message "Please click back again to exit the application". i was wonder when i saw it first time and tried to implement same logic in my own android application and now finally i knew it how we can achieve this.so here in this post i would like to share this awesome code.
You need to implement the below code on onBackPressed() override method:
You need to implement the below code on onBackPressed() override method:
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);
}
No comments :
Post a Comment