简体   繁体   中英

What is the difference between onBackPressed and onCancel in DialogFragment?

When trying to use the back button of a dialog fragment

I'm curious as to the difference between using onBackPressed() (or onBackPressedCallBack ) and onCancel() .

I tried to define the data forwarding event of the back key using onBackPressedCallback() and OnBackPressedDispatcher() in the dialog fragment , but it didn't work.

In the end, I passed the data using onCancel() .

Why doesn't the back key with onBackPressed() work?

UPDATED

public class WritingCommentDialogFragment extends DialogFragment {
    OnBackPressedCallback callback;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        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);
    }

}

onBackPressed() of the dialog itself intercepts the back button press before the host Activity ever gets a shot at it. The default behavior is to call cancel() on the dialog and not pass it on to the Activity. That's why adding your callback to the Activity isn't doing anything. If you want to specifically handle the back button press, you would need to subclass Dialog to override it and use that class as the Dialog you create in onCreateDialog() . Something like this:

public class MyDialogFragment extends DialogFragment {

    @NotNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = new Dialog(requireContext(), getTheme()) {
            @Override
            public void onBackPressed() {
                MyDialogFragment.this.onBackPressed();
                // And maybe you also want to call cancel() here.
            }
        };
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    private void onBackPressed() {
        // your code here
    }
}

A dialog is canceled not only when back is pressed, but also if the user taps outside the dialog or presses the cancel button (if you added one to the dialog), or if you manually call cancel() on it or its host DialogFragment. So performing your action in onCancel() covers more events than just the back button being pressed.

A third possible event you can hook onto (with an OnDismissListener) is the dialog being dismissed, which is any time it is closed, including when it is canceled or when the screen rotates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM