Material-dialogs: Question. Dismissing simple list dialog?

Created on 5 Sep 2016  路  4Comments  路  Source: afollestad/material-dialogs

How should I dismiss simple list dialog on item click?


final MaterialSimpleListAdapter adapter = new MaterialSimpleListAdapter(new MaterialSimpleListAdapter.Callback() {
    @Override
    public void onMaterialListItemSelected(int index, MaterialSimpleListItem item) {
        // dismiss dialog here
    }
});

adapter.add(new MaterialSimpleListItem.Builder(this)
    .content(R.string.add_account)
    .icon(R.drawable.ic_content_add)
    .iconPaddingDp(8)
    .build());

new MaterialDialog.Builder(this)
    .title(R.string.set_backup)
    .adapter(adapter, null)
    .show();

How can I get dialog instance in callback to dismiss it?

questiodiscussion

Most helpful comment

@afollestad Would you please consider adding a dialog parameter again?

With the current API, there's the following situation: We need to create the MaterialSimpleListAdapter and therefore also the MaterialSimpleListAdapter.Callback _before_ calling build(). I don't see any chance of getting hold of the created MaterialDialog instance before creating the callback. However, this reference would be required for probably the most common action - closing the dialog before proceeding.

The workaround mentioned by @palaima is to store the dialog reference in a member (or static) variable. That's working, but unless we add some cleanup code to the dismiss handler, it means we're keeping this reference and therefore also references its views and its Context (probably an Activity) alive. Sure, there are other ways to do it, but it can get very complicated. Considering that pretty much every action will dismiss the dialog and MaterialSimpleListAdapter already holds the reference in a private field, it would be great if it could pass it on to the callback.

All 4 comments

One possible solution:

private Dialog dialog;
***
***

final MaterialSimpleListAdapter adapter = new MaterialSimpleListAdapter(new MaterialSimpleListAdapter.Callback() {
    @Override
    public void onMaterialListItemSelected(int index, MaterialSimpleListItem item) {
        if (dialog != null)
            dialog.dismiss();
    }
});

dialog = new MaterialDialog.Builder(this)
    .title(R.string.set_backup)
    .adapter(adapter, null)
    .build();

dialog.show();

Are there more ways to do that?

Similar question here... the old API used to provide the MaterialDialog reference to ListCallback.onSelection(), so the dialog could easily be dismissed. The new API is missing that.

@afollestad Would you please consider adding a dialog parameter again?

With the current API, there's the following situation: We need to create the MaterialSimpleListAdapter and therefore also the MaterialSimpleListAdapter.Callback _before_ calling build(). I don't see any chance of getting hold of the created MaterialDialog instance before creating the callback. However, this reference would be required for probably the most common action - closing the dialog before proceeding.

The workaround mentioned by @palaima is to store the dialog reference in a member (or static) variable. That's working, but unless we add some cleanup code to the dismiss handler, it means we're keeping this reference and therefore also references its views and its Context (probably an Activity) alive. Sure, there are other ways to do it, but it can get very complicated. Considering that pretty much every action will dismiss the dialog and MaterialSimpleListAdapter already holds the reference in a private field, it would be great if it could pass it on to the callback.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eygraber picture eygraber  路  3Comments

sarthak1996 picture sarthak1996  路  6Comments

CHOIMINSEOK picture CHOIMINSEOK  路  8Comments

jlelse picture jlelse  路  6Comments

ghost1372 picture ghost1372  路  5Comments