1

I have an alert dialog, I want to make non cancellable, if the user clicks back button on the device, the dialog is gone. how do i stop it from doing that.

I have added setCancelable(false) but it is not working.

Any thoughts on how to fix this please

Here is my code.

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    app.bus.post(ScreenDimEvent(false))

    val builder = AlertDialog.Builder(activity)

    val inflater = activity!!.layoutInflater

    @SuppressLint("InflateParams")
    val inflatedView = inflater.inflate(R.layout.dialog_quality_control, null)

    ButterKnife.bind(this, inflatedView)

    builder
            .setView(inflatedView)
            .setCancelable(false)
            .setTitle(getString(R.string.quality_control) + " - " + qualityControlCheck.name)
            .setPositiveButton(R.string.fuel_order_signature_dialog_save) { dialog, id ->
                validator.validate()
            }

    presenter.setView(this)
    presenter.init(fuelOrderId, qualityControlCheck)

    dialog = builder.create()
    dialog.setCancelable(false)//TRIED THIS BUT DID NOT WORK
    dialog.setCanceledOnTouchOutside(false)

    return dialog
}
BRDroid
  • 3,920
  • 8
  • 65
  • 143

2 Answers2

1

Check answers from this thread:

Prevent back button from closing a dialog box

Especially this answer: https://stackoverflow.com/a/28111013/4350431

alertDialog?.setOnKeyListener { dialog, keyCode, event -> keyCode == KeyEvent.KEYCODE_BACK }
  • Hey thanks, how to write this piece of code in kotlin please https://stackoverflow.com/a/28111013/4350431 alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { // Prevent dialog close on back press button return keyCode == KeyEvent.KEYCODE_BACK; } }); – BRDroid Mar 12 '20 at 15:35
0

Add to the dialog configuration, after builder.create();

dialog.setCancelable(false);
Ollie Strevel
  • 861
  • 1
  • 14
  • 27