gpt4 book ai didi

android - DialogFragment 和 onDismiss

转载 作者:IT王子 更新时间:2023-10-28 23:42:50 24 4
gpt4 key购买 nike

我正在使用 DialogFragment,我在 Activity 中显示如下:

DialogFragmentImage dialog = DialogFragmentImage.newInstance(createBitmap());
dialog.onDismiss(dialog);.onDismiss(this);
dialog.show(getFragmentManager(), "DialogFragmentImage");

我想检查 DialogFragment 何时被解除(例如,当按下后退按钮时),但在我的 Activity 中。我怎样才能做到这一点?如何“告诉”我的 activity DialogFragment 已被解除?

最佳答案

让你的Activity实现OnDismissListener

public final class YourActivity extends Activity implements DialogInterface.OnDismissListener {

@Override
public void onDismiss(final DialogInterface dialog) {
//Fragment dialog had been dismissed
}

}

DialogFragment已经实现了OnDismissListener,只需重写方法调用Activity即可。

public final class DialogFragmentImage extends DialogFragment {

///blah blah

@Override
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
final Activity activity = getActivity();
if (activity instanceof DialogInterface.OnDismissListener) {
((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
}
}

}

如果您使用 childFragment 管理器 (API>=17) 从 fragment 启动对话框,则可以使用 getParentFragment 与父级上的 onDismissListener 对话 fragment :

public final class DialogFragmentImage extends DialogFragment {

///blah blah

@Override
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
Fragment parentFragment = getParentFragment();
if (parentFragment instanceof DialogInterface.OnDismissListener) {
((DialogInterface.OnDismissListener) parentFragment).onDismiss(dialog);
}
}

}

关于android - DialogFragment 和 onDismiss,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com