DialogFragment and onDismiss

前端 未结 8 1980
迷失自我
迷失自我 2020-11-27 16:14

I am using a DialogFragment, which I am showing like this from an Activity:

DialogFragmentImage dialog = DialogFragmentImage.newIns         


        
8条回答
  •  误落风尘
    2020-11-27 16:27

    Here is my answer. It's a bit late but it's maybe benefit someone passing by.

    FragmentManager fm = getFragmentManager();
    
    YourDialogFragment dialog = new YourDialogFragment();
    dialog.show(fm,"MyDialog");
    
    fm.executePendingTransactions();
    dialog.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {
                           //do whatever you want when dialog is dismissed
                        }
                    });
    

    We need to call

    fm.executePendingTransactions(); 
    

    To make sure that FragmentTransaction work has been performed. Otherwise NullPointerException can occur when calling setOnDismissListener().

    Sorry if there is any mistake. Hope this help.

提交回复
热议问题