August 31 2015

Android SDK click twice back button to exit

Tagged Under :

Android
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;

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

Make a Comment

You must be logged in to post a comment.