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();
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.
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 blockcancel().