android-activity

overriding back button

Double back to close application

You may have noticed that many applications have double-back-click functionality to exit the app. In this example, we are overriding the default back button action using the onBackPressed() method override.

This method will Toast a message for the single back-click action, and will close the app if the user clicks the back button twice within two seconds. We use a Handler for the functionality that relies on the two-second interval.

boolean singleBack = false;

@Override
public void onBackPressed() {
   if (singleBack) {
    super.onBackPressed();
    return;
   }

   this.singleBack = true;
   Toast.makeText(this, "Double Back to exit", Toast.LENGTH_SHORT).show();

   new Handler().postDelayed(new Runnable() {

      @Override
      public void run() {
          singleBack=false;                       
      }
   }, 2000);
} 

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow