Not working onbackpressed when setcancelable of alertdialog is false

The easiest work-around for this problem is to set an OnKeyListener and automatically detect when the user hits the back button.

Java:

public Dialog onCreateDialog(Bundle savedInstanceState) {

  Dialog dialog = super.onCreateDialog(savedInstanceState);

  dialog.setOnKeyListener(new Dialog.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
        if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP) {
          dialog.cancel;
          return true;
        }
        return false;
    }
  });

  return dialog;
}

Kotlin:

    dialog = AlertDialog.Builder(this)
            .setCancelable(false)
            .create()
    dialog.show()

    dialog.setOnKeyListener (object : Dialog.OnKeyListener { 
      override fun onKey(dialogInterface: DialogInterface, keyCode: Int, keyEvent: KeyEvent) {
        if(keyCode == KeyEvent.KEYCODE_BACK and keyEvent.action == KeyEvent.ACTION_UP) {
            dialog.dismiss()
            true
        }
        false
      }})

Note that I added an extra condition in the if-statement, all this does is to make sure this does not fire twice.

I hope this helps you.


As i see you create dialogBuilder is public why not you call this in a public alertDialog and than show it using alertDilog.show() and close on back pressClick in activity and dismiss the dialog alertDilog.dismiss() override the onBackPress and dismiss it here

val alertDialog:AlertDialog?=null


         alertDialog = new AlertDialog.Builder(this)
        //set icon 
        .setIcon(android.R.drawable.ic_dialog_alert)
        //set title
        .setTitle("Are you sure to Exit")
        //set message
        .setMessage("Exiting will call finish() method")
        //set positive button
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        //set what would happen when positive button is clicked    
            finish();
        }
        })
        //set negative button
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        //set what should happen when negative button is clicked
            Toast.makeText(getApplicationContext(),"Nothing 
          Happened",Toast.LENGTH_LONG).show();
         }
        })
        .show();

       onBackPress(){alertDialog.dismiss()}

just add a onKeyListener and cancel dialog on back key event.

    lateinit var dialog: AlertDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        dialog = AlertDialog.Builder(this).setMessage("Check your internet connection")
                .setPositiveButton("OK") { _, _ ->
                    Toast.makeText(this, "OK", Toast.LENGTH_LONG).show()
                }.setCancelable(false)
                .create()
        dialog.show()

        dialog.setOnKeyListener { _, keyCode, _ ->
            if(keyCode == KeyEvent.KEYCODE_BACK) {
                if(dialog.isShowing) {
                    dialog.dismiss()
                }
            }
            true
        }
    }