使用 OnBackPressedDispatcher() 的 OnBackPressedCallback() 不起作用
OnBackPressedCallback() using OnBackPressedDispatcher() does not work
我通过单击 activity 中实现的 recycler view item
打开了 dialog fragment
。
而对于 back function
我使用了 onBackPressedCallback()
。
然而,我试图显示一个 toast message
进行测试,但没有任何反应。
我参考了 Android 开发者文档。
有什么问题?
WC.java (DialogFragment)
public class WritingCommentDialogFragment extends DialogFragment implements CommentModel.EditInputListener {
OnBackPressedCallback callback;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
bindViews(view);
addCommentItem();
Toast.makeText(getContext(), "onCreateView()", Toast.LENGTH_SHORT).show();
return view;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Toast.makeText(getContext(), "TEST", Toast.LENGTH_SHORT).show();
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
@Override
public void onDetach() {
super.onDetach();
callback.remove();
}
@Override
public void onResume() {
super.onResume();
setDialogSize();
}
}
根据this issue:
Dialogs are separate windows that always sit above your activity's window. This means that the dialog will continue to intercept the system back button no matter what state the underlying FragmentManager is in, or what code you run in your Activity's onBackPressed()
- which is where the OnBackPressedDispatcher
plugs into.
如果将此代码放入 Activity 的 onBackPressed()
中,您会遇到同样的问题 - Activity 不再是 window 接收关键事件(其中系统后退按钮是其中之一),因此预计不会触发任何插入其中的东西。
当对话框打开时,唯一接收系统后退按钮调用的地方是对话框的 onBackPressed()
method,这意味着创建您自己的 Dialog
子类(而不是依赖 super.onCreateDialog()
) 并手动覆盖该方法。
当然,如果您只想在对话框关闭时进行回调(即对话框打开时系统后退按钮的默认行为),那么您需要重写 DialogFragment
的onCancel()
method.
我通过单击 activity 中实现的 recycler view item
打开了 dialog fragment
。
而对于 back function
我使用了 onBackPressedCallback()
。
然而,我试图显示一个 toast message
进行测试,但没有任何反应。
我参考了 Android 开发者文档。
有什么问题?
WC.java (DialogFragment)
public class WritingCommentDialogFragment extends DialogFragment implements CommentModel.EditInputListener {
OnBackPressedCallback callback;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
bindViews(view);
addCommentItem();
Toast.makeText(getContext(), "onCreateView()", Toast.LENGTH_SHORT).show();
return view;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Toast.makeText(getContext(), "TEST", Toast.LENGTH_SHORT).show();
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
@Override
public void onDetach() {
super.onDetach();
callback.remove();
}
@Override
public void onResume() {
super.onResume();
setDialogSize();
}
}
根据this issue:
Dialogs are separate windows that always sit above your activity's window. This means that the dialog will continue to intercept the system back button no matter what state the underlying FragmentManager is in, or what code you run in your Activity's
onBackPressed()
- which is where theOnBackPressedDispatcher
plugs into.
如果将此代码放入 Activity 的 onBackPressed()
中,您会遇到同样的问题 - Activity 不再是 window 接收关键事件(其中系统后退按钮是其中之一),因此预计不会触发任何插入其中的东西。
当对话框打开时,唯一接收系统后退按钮调用的地方是对话框的 onBackPressed()
method,这意味着创建您自己的 Dialog
子类(而不是依赖 super.onCreateDialog()
) 并手动覆盖该方法。
当然,如果您只想在对话框关闭时进行回调(即对话框打开时系统后退按钮的默认行为),那么您需要重写 DialogFragment
的onCancel()
method.