AlertDialog setOnDismissListener not working

前端 未结 6 1372
孤独总比滥情好
孤独总比滥情好 2021-02-19 11: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 get

相关标签:
6条回答
  • 2021-02-19 12:24
    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();
            .....
        }
    }
    
    0 讨论(0)
  • 2021-02-19 12:30

    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;
                }
            })
    
    0 讨论(0)
  • 2021-02-19 12:31

    I found the real problem.

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

    Try it :)

    0 讨论(0)
  • 2021-02-19 12:44

    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     
                        }
                    });
    
    0 讨论(0)
  • 2021-02-19 12:45

    OK...I figured it out myself.

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

    0 讨论(0)
  • 2021-02-19 12:46

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

    0 讨论(0)
提交回复
热议问题