android tutorial - Double back in Android Activity | Developer android - android app development - android studio - android app developement



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);
} 
click below button to copy code from our android learning website - android tutorial - team

Related Searches to Double back in Android Activity