lumpawire
lumpawire

Reputation: 468

AlertDialog setOnDismissListener not working

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

Upvotes: 19

Views: 45770

Answers (6)

Vishal Nagvadiya
Vishal Nagvadiya

Reputation: 1278

Use following code

final AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
                final View dailogView = LayoutInflater.from(MyActivity.this).inflate(R.layout.dialog_layout, null);
                builder.setView(dailogView);
                final AlertDialog dialog=builder.create();
                dialog.show();

DismissListener

 dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                   // your code after dissmiss dialog     
                    }
                });

Upvotes: 4

lumpawire
lumpawire

Reputation: 468

OK...I figured it out myself.

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

Upvotes: 4

Java Geek
Java Geek

Reputation: 344

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;
            }
        })

Upvotes: 9

RogerParis
RogerParis

Reputation: 1559

I found the real problem.

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

Try it :)

Upvotes: 2

Zeev G
Zeev G

Reputation: 2211

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

Upvotes: 13

Yahor10
Yahor10

Reputation: 2132

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

Upvotes: 3

Related Questions