Display Alert on Back Button Pressed in Android Studio


When a user wants to exit from an application, he is prompted to exit from the application.
Alert means to get the user's input when needed. Most of us have played games and when we try to exit it by pressing the "Back" button on an Android phone, it will ask "Do you really want to exit?". So, this article explains how to prevent the user from exiting an application without giving a response.
Step 1
Create a new project with the following parameters.
Step 2
Open your Main Activity file, in my case, it is "BackPressActivity" and pastes the following code into it.
  1. package com.example.alertonbackpressdemo;  
  2. import android.app.AlertDialog;  
  3. import android.content.DialogInterface;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.view.Menu;  
  8. public class BackPressActivity extends Activity {  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14.     @Override  
  15.     public void onBackPressed() {  
  16.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  17.         builder.setCancelable(false);  
  18.         builder.setMessage("Do you want to Exit?");  
  19.         builder.setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
  20.             @Override  
  21.             public void onClick(DialogInterface dialog, int which) {  
  22.                 //if user pressed "yes", then he is allowed to exit from application  
  23.                 finish();  
  24.             }  
  25.         });  
  26.         builder.setNegativeButton("No",new DialogInterface.OnClickListener() {  
  27.             @Override  
  28.             public void onClick(DialogInterface dialog, int which) {  
  29.                 //if user select "No", just cancel this dialog and continue with app  
  30.                 dialog.cancel();  
  31.             }  
  32.         });  
  33.         AlertDialog alert=builder.create();  
  34.         alert.show();  
  35.     }     
Let's understand the Alert Dialog and its various methods.
In code, we use "builder.setCancelable(false);" so that user can't cancel this dialog by pressing back again or touch outside the alert dialog box and dismiss it. So the user must press one of the options you have provided.
Next is "builder.setMessage("Do you want to Exit?");". Here you can write your own message that will be displayed to the user in an alert.
"builder.setPositiveButton" will set a positive button on the left side. The parameter will accept the name of that button as you can see in code it is "Yes". The same thing is set for "builder.setNegativeButton". When you set these buttons, you need to pass a listener that will be fired when the user clicks one of the buttons.
Step 3
Run your application.


In this article, we learned "How to show Alert Dialog" and "Show dialog at BACK button pressed".

Similar Articles