Android setOnDismissListener导致在配置更改后重新显示已解除的对话框片段

Android setOnDismissListener导致在配置更改后重新显示已解除的对话框片段,android,android-dialogfragment,android-dialog,Android,Android Dialogfragment,Android Dialog,在我的Fragment的onCreateView()方法中,我调用我的showHelp()方法来显示一个对话框Fragment,使用一个OnDismissListener,以便在对话框关闭时可以执行一些其他代码: protected void showHelp(DialogInterface.OnDismissListener onHelpDismissListener) { String helpTitle = getHelpTitle(); String helpConte

在我的Fragment
onCreateView()
方法中,我调用我的
showHelp()
方法来显示一个对话框Fragment,使用一个OnDismissListener,以便在对话框关闭时可以执行一些其他代码:

protected void showHelp(DialogInterface.OnDismissListener onHelpDismissListener) {

    String helpTitle = getHelpTitle();
    String helpContent = getHelpContent();

    InfoDialog helpDialog = InfoDialog.newInstance(helpTitle, helpContent);
    helpDialog.showNow(getChildFragmentManager(), InfoDialog.TAG);
    Dialog dialog = helpDialog.getDialog();

    Log.d(TAG, "For helpDialog dialog, setting dismiss listener: " + onHelpDismissListener);
    if (onHelpDismissListener != null) {

        /*
         * When I remove this line, InfoDialog behaves properly - i.e., if it is dismissed, then
         * the device rotated, it is not displayed again.
         * 
         * Even when the onHelpDismissListener's `onDismiss()` method performs no action, the problem still occurs.
         */
        dialog.setOnDismissListener(onHelpDismissListener);
    }
}
但是,包括
dialog.setOnDismissListener(onHelpDismissListener)行导致对话框在配置更改后再次显示在UI上,尽管它以前已被取消

如果我删除该行,它的行为应该是这样的。(但是,很明显,我没有收到回电。)

为了完整起见,这里是我的信息对话框课程:

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.ViewModelProvider;

public class InfoDialog extends DialogFragment {

    public static final String TAG = InfoDialog.class.getSimpleName();
    private static final String ARG_TITLE = "ARG_TITLE";
    private static final String ARG_MESSAGE = "ARG_MESSAGE";

    private InfoDialogViewModel mInfoDialogViewModel;

    public static InfoDialog newInstance(String title, String message) {

        InfoDialog infoDialog = new InfoDialog();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_MESSAGE, message);
        infoDialog.setArguments(args);
        return infoDialog;
    }

    @Override @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction

        Bundle args = getArguments();
        if (args == null) {
            throw new RuntimeException("Null args!");
        }

        Context context = requireContext();

        mInfoDialogViewModel = new ViewModelProvider(requireActivity()).get(InfoDialogViewModel.class);

        String title = args.getString(ARG_TITLE);
        String message = args.getString(ARG_MESSAGE);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(Utilities.fromHtml(context, title, this, null));
        builder.setMessage(Utilities.fromHtml(context, message, this, null));
        builder.setPositiveButton(R.string.ok, (DialogInterface dialog, int id) -> {
            mInfoDialogViewModel.setResult(InfoDialogViewModel.RESULT_OK);
        });

        builder.setCancelable(true);
        builder.setOnCancelListener((DialogInterface dialog) -> {
            mInfoDialogViewModel.setResult(InfoDialogViewModel.RESULT_CANCELLED);
        });

        // Create the AlertDialog object and return it
        return builder.create();
    }

}
你知道为什么会发生这个问题吗?我怎样才能防止配置更改后被取消的对话框重新出现


或者,是否有其他方法来侦听被取消的对话框?

DialogFragment
对话框上设置自己的
OnDismissListener
,并在那里处理
片段的删除。当您在
对话框
上设置
OnDismissListener
时,它会覆盖
DialogFragment
的侦听器,这样就不会发生删除操作,
碎片管理器
认为
DialogFragment
在配置更改后仍然处于活动状态。相反,您需要重写DialogFragment的
onDismiss()
,以获取dismise事件,然后在那里处理您的侦听器,不管您喜欢什么;e、 例如,在
InfoDialog
中创建一个
setOnDismissListener()
方法,并从
onDismiss()
调用该侦听器。啊,当然了!:-)现在一切都好了。如果你想加上这个作为回答,我会接受的。谢谢。
DialogFragment
对话框上设置自己的
ondismissleener
,并在那里处理
片段的删除。当您在
对话框
上设置
OnDismissListener
时,它会覆盖
DialogFragment
的侦听器,这样就不会发生删除操作,
碎片管理器
认为
DialogFragment
在配置更改后仍然处于活动状态。相反,您需要重写DialogFragment的
onDismiss()
,以获取dismise事件,然后在那里处理您的侦听器,不管您喜欢什么;e、 例如,在
InfoDialog
中创建一个
setOnDismissListener()
方法,并从
onDismiss()
调用该侦听器。啊,当然了!:-)现在一切都好了。如果你想加上这个作为回答,我会接受的。非常感谢。