AlertDialog setOnDismissListener not working

2019-04-06 07:44发布

My activity opens a dialog. When it closes I need the function ReloadTable() to be executed. So I am trying to use setOnDismissListener but its not getting triggered. Could someone please help what I am doing wrong?

Thanks!

AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.transaction, null);
builder = new AlertDialog.Builder(new ContextThemeWrapper(TransactionsList.this , R.style.dialogwithoutdim));
builder.setView(layout);
alertDialog = builder.create();
alertDialog.setOnDismissListener(new OnDismissListener() {
    public void onDismiss(final DialogInterface dialog) {
        ReloadTable();
    }
});

builder.show();

5条回答
神经病院院长
2楼-- · 2019-04-06 08:05

I found the real problem.

You should call .show in the dialog, not in the builder.

Try it :)

查看更多
Lonely孤独者°
3楼-- · 2019-04-06 08:09

OK...I figured it out myself.

I had to implement DialogInterface.OnCancelListener and add the onCancel method. It worked!

查看更多
beautiful°
4楼-- · 2019-04-06 08:15

You have to setOnCancelListener to the AlertDialog.Builder:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);
alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialogmenu = false;
            }
        })
查看更多
时光不老,我们不散
5楼-- · 2019-04-06 08:18
public class MyActivity extends Activity implements DialogInterface.OnCancelListener{
    @Override
    public void onCreate(Bundle state) {
       .....
       alertDialog.setOnCancelListener(this);
       alertDialog.show();
    }
    @Override
    public void onCancel(DialogInterface dialog) {
        dialog.dismiss();
        .....
    }
}
查看更多
淡お忘
6楼-- · 2019-04-06 08:19

In this case you should use alertDialog.setOnCancelListener(listener),and alertDialog.setOnDismissListener works with dismissDialog(id).

查看更多
登录 后发表回答