6

My code displays an AlertDialog, which exits the activity by pressing the positive button. I want it to be able to exit on back button as well. But my onBackPressed does not work when I have .setCancelable(false) . How do I fix this without changing it to .setCancelable(true)

// show in dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("BROADCAST")
            .setMessage(text)
            .setCancelable(false)
            .setPositiveButton("Dismiss",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            finish();
                        }
                    });
    AlertDialog alert = builder.create();
    alert.show();
}

@Override
public void onBackPressed() {
    finish();
}
user352806
  • 173
  • 1
  • 3
  • 9
  • So you want to close the activity on a back press if the dialog is opened? – Basti Destruction Mar 04 '17 at 18:05
  • yes, that's exactly what I want – user352806 Mar 04 '17 at 18:06
  • finish replace with dismiss – aslamhossin Mar 04 '17 at 18:10
  • If you don't set set cancelable to true, activity will never receive the onbackpressed callback when user presses back button. I wonder there is really a way to do this – siva Mar 04 '17 at 18:12
  • I had the same problem. But I was programming in Kotlin. If somebody uses Kotlin, my question will help him/her:https://stackoverflow.com/questions/60462748/not-working-onbackpressed-when-setcancelable-of-alertdialog-is-false – MMG May 18 '20 at 07:09

2 Answers2

6

easy peeasy..

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("BROADCAST")
        .setMessage(text)
        .setCancelable(true)
        .setPositiveButton("Dismiss",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                })
        .setOnDismissListener(new DialogInterface.OnDismissListener() {
                                @Override
                                public void onDismiss(DialogInterface dialog) {
                                    finish();
                                }
                            });
AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(false);
alert.show();

For answer in Kotlin see here:Not working onbackpressed when setcancelable of alertdialog is false

MMG
  • 3,226
  • 5
  • 16
  • 43
Abhishek Singh
  • 9,008
  • 5
  • 28
  • 53
  • why down vote he want to close activity with either positive button either back pressed, this code does same thing. just set `cancelable(true)`... it will receive on dismiss... and `setCanceledOnTouchOutside(false);` – Abhishek Singh Mar 04 '17 at 18:15
  • I was actually trying to do the exact same thing you did but I was bombed with errors. I don't understand why people are so ignorant. This is the right solution. – user352806 Mar 04 '17 at 18:18
  • Cause I manipulated the answer you want to `setCancelable(false)`. I chenged `setCancelable(true)`. they downvoted me – Abhishek Singh Mar 04 '17 at 18:21
  • In the question it is mentioned that how to achieve this without setcancelable(true)... I guess the down vote is because of that...however your answer is correct. I could not understand how this answer has been accepted. According to the question, @Majeed khan answer seems convincing – siva Mar 05 '17 at 06:54
  • @7383 acutally my answer fulfill his need. he asked wrong question his question should be close activity either positive button clicked or dialog closed. – Abhishek Singh Mar 05 '17 at 15:07
  • I had the same problem. But I was programming in Kotlin. If somebody uses Kotlin, my question will help him/her:https://stackoverflow.com/questions/60462748/not-working-onbackpressed-when-setcancelable-of-alertdialog-is-false – MMG May 18 '20 at 07:10
4

you can set a key listener

builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                    if(keyCode == KeyEvent.KEYCODE_BACK){
                        dialog.dismiss(); // dismiss the dialog
                       YourActivity.this.finish(); // exits the activity

                    }

                    return true;
                }
            })
Majeed Khan
  • 505
  • 7
  • 16
  • this is not how it works, dialog can override back button and onBackPressed – user352806 Mar 04 '17 at 18:28
  • It would probably work, but not when an AlertDialog is being displayed – user352806 Mar 04 '17 at 18:29
  • eidted the answer if you want to exit the activity then you can simply do it by getting the activity instance and calling finsih() on the instance – Majeed Khan Mar 04 '17 at 18:33
  • and one more thing! AlertDialog does not override the onBackPressed! it is overridden by the activity itself. – Majeed Khan Mar 04 '17 at 18:38
  • I had the same problem. But I was programming in Kotlin. If somebody uses Kotlin, my question will help him/her:https://stackoverflow.com/questions/60462748/not-working-onbackpressed-when-setcancelable-of-alertdialog-is-false – MMG May 18 '20 at 07:10