问题描述
我使用以下代码显示一个 BottomSheetDialogFragment:
BottomSheetDialogFragment bottomSheetDialogFragment = new MediaAddFragment();
bottomSheetDialogFragment.show(getActivity().getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
getActivity().getSupportFragmentManager().executePendingTransactions();
bottomSheetDialogFragment.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
onResume();
MainActivity.updateMediaButtons();
}
});
为了关闭它,我从 Fragment 内部调用了dismiss()。 这样,它就会被关闭,但如果应用程序恢复,它会再次显示,这不是我的意图。
如果有人可以帮助我,我会很高兴。 我已经扫描了有关如何正确使用这些 BottomSheetDialogFragments 的各种教程,但我找不到我的错误。
顺便说一句,我在 onResume 中没有任何代码来测试它。
1楼
(代表 OP 发布) 。
我发现了我的错误,我在调用片段中覆盖了 BottomSheetDialogFragment 的 onDismissListener。 现在它按预期工作。
bottomSheetDialogFragment2.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// Adding the following line fixed the problem for me
bottomSheetDialogFragment2.onDismiss(dialog);
// some Code....
}
});
2楼
我从 15 分钟就遇到了这个问题,问题是我没有调用 onDismiss of sheet
override fun onDismiss(dialog: DialogInterface) {
if (shouldDismiss)
onDismiss.invoke()
// super.onDismiss(dialog) // this solution (final code)
}
所以在你的代码中应该是
@Override
public void onDismiss(DialogInterface dialog) {
onResume();
MainActivity.updateMediaButtons();
super.onDismiss(dialog);
}