5

Display Alert on Back button pressed in Android Studio

 1 year ago
source link: https://chintanrathod.com/display-alert-on-back-button-pressed-in-android-studio/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Display Alert on Back button pressed in Android Studio

Some time in your application, it is needed to prompt user before taking back him to previous page or exit from application. In this article, we will see how you can prompt user on pressing back button.

Alert means to get user’s input when needed. Most of us played games and when we try to exit it by pressing “Back” button in Android phone, it ask for “Do you really want to exit?”.

So, in this article we are going to learn how to prevent user to exit from application without giving response.

Step 1

Create a new Project with following parameters.

alert back press 1

Step 2

Open your Main Activity file, in my case it is “BackPressActivity.java” and paste following code inside.

[java]
package com.example.alertonbackpressdemo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
/**
* BackPressActivity will show alert dialog when user
* presses back button.
*
* @author Chintan Rathod (http://chintanrathod.com)
*/
public class BackPressActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do you want to Exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
[/java]

Let’s understand Alert Dialog and its different methods.

  1. builder.setCancelable(false); which means that user can’t cancel this dialog by pressing back again or touch outside the alert dialog box and dismiss it. So user has to press one of the options you have provided to him.
  2. builder.setMessage(“Do you want to Exit?”); Here you can write your own message which will be displayed to user in alert.
  3. builder.setPositiveButton will set a positive button in left side. Parameter will accept the name of that button as you can see in code is “Yes”.
  4. builder.setNegativeButton will set a negative button in right side. Parameter will accept the name of that button as you can see in code is “No”

When you set this button, you need to pass a listener which will be fired when user click one of the button.

Step 3

Run your application.

Summary

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK