Double back press exit android app

Sometimes it is required to block accidental back press on android app. The following code snippet helps to block accidental back press, It also helps to exit the app by double back pressing. Get back press work only at second press and notify user to press again to exit use the following snippet of codes.
[java]
private static long back_pressed;
@Override
public void onBackPressed(){
if (back_pressed + 2000 > System.currentTimeMillis()){
super.onBackPressed();
}
else{
Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = System.currentTimeMillis();
}

}
[/java]

Incoming search terms:

  • android double back press
  • double back to exit android
  • exit application by pressing again
  • Press again to exit
  • press again to exit in android

Tushar Roy

Software Engineer (Backend engineer), Android Developer,

One thought on “Double back press exit android app

Leave a Reply

Your email address will not be published. Required fields are marked *


*