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?
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.
Most helpful comment
@afollestad Would you please consider adding a
dialogparameter again?With the current API, there's the following situation: We need to create the
MaterialSimpleListAdapterand therefore also theMaterialSimpleListAdapter.Callback_before_ callingbuild(). I don't see any chance of getting hold of the createdMaterialDialoginstance 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 anActivity) 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 andMaterialSimpleListAdapteralready holds the reference in a private field, it would be great if it could pass it on to the callback.