Material-dialogs: dialog.cancel() is working

Created on 1 May 2016  路  6Comments  路  Source: afollestad/material-dialogs

How can i cancel dialog?
cancel() method not working...

MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
    dialog.content("Loading...").cancelListener(new DialogInterface.OnCancelListener() {
         @Override
         public void onCancel(DialogInterface dialog) {
              dialog.cancel();
         }
}).progress(true, 100).cancelable(false).show();

MaterialDialog dialog = builder.build();
dialog.cancel();
questiodiscussion

Most helpful comment

I'm not sure what could be wrong, none of the other thousands of people using this library have reported anything wrong.

Although I did notice you set cancelable(false) in one instance above which would block cancel().

All 6 comments

This wouldn't have anything to do with this library. cancel(), dismiss(), show(), etc. are not methods I created. Their from android.app.Dialog.

You should use dismiss() to close dialogs, though.

Thanks @afollestad , Great work.

I'm not sure what could be wrong, none of the other thousands of people using this library have reported anything wrong.

Although I did notice you set cancelable(false) in one instance above which would block cancel().

I already closed the issue, by the way. Not much can be done on my end.

new MaterialDialog.Builder(MyActivity.this)
    .title("Title")
    .content("Content")
    .positiveText(R.string.ok)
    .onPositive(new MaterialDialog.SingleButtonCallback() {
        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            dialog.dismiss();
            startActivityForResult(intent);
        }
    }).show();

Here's how I built the dialog and still experiencing the same issue they reported. I understand its not much to do with you as you're using the default behaviour. I just pointed out that it doesn't get dismissed or cancelled even if the cancelable(false) is not set.

Somehow I figured out to get my problem solved. So I feel like sharing here.

new MaterialDialog.Builder(MyActivity.this)
    .title("Title")
    .content("Content")
    .positiveText(R.string.ok)
    .onPositive(new MaterialDialog.SingleButtonCallback() {
        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            dialog.dismiss();
            startActivityForResult(intent);
        }
    }).show();

This implementation was not working. Cancelling or dismissing the dialog from inside the onClick function was not working for me. So I created an instance of the MaterialDialog and called build

private void initializeDialog() {
    mDialogBuilder = new MaterialDialog.Builder(MainActivity.this)
        .title("Title")
        .content("Content")
        .cancelable(false)    // cancelable is set to false
        .positiveText(R.string.ok)
        .onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                // Removed the cancel/dismiss call from here. 
                startActivityForResult(intent);
            }
        });

    // Build the dialog
    mDialog = mDialogBuilder.build();
}

Then I could show and dismiss/cancel the dialog using

mDialog.show();  // Show the dialog
mDialog.dismiss()  // Dismiss  the dialog

This is strange but working and I don't know why. May the the reference of the MaterialDialog passed to onClick of MaterialDialog.SingleButtonCallback has the wrong reference.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MFlisar picture MFlisar  路  3Comments

ghost picture ghost  路  4Comments

jahirfiquitiva picture jahirfiquitiva  路  6Comments

pixelbendr picture pixelbendr  路  5Comments

Supercaly picture Supercaly  路  3Comments