Java
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); }
Java
private boolean doubleBackToExitPressedOnce; @Override public void onBackPressed() { if (webView.canGoBack()) { webView.goBack(); } else { if (doubleBackToExitPressedOnce) { super.onBackPressed(); return; } this.doubleBackToExitPressedOnce = true; Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; } }, 2000); // Time interval for the double back press (2 seconds in this example) } }
Tags:
Android Studio