August 31 2015

Android SDK click twice back button to exit

Tagged Under :

A lot of Android users have experience accidentally exit the application when he click the back button.

To avoid this happen again. you can modify back button action to let user click twice before exit.
press again to exit

And also, you can display message “Press again to exit” to ask the user click again to exit the application.

You can implement the code as below inside the application.
private Boolean exit = false;

public void onBackPressed() {
	if (exit) {
	} else {
		Toast.makeText(this, "Press AGAIN to EXIT",
		exit = true;
		new Handler().postDelayed(new Runnable() {
			public void run() {
				exit = false;
		}, 3 * 1000);

Make a Comment