ybybyb
ybybyb

Reputation: 1739

OnBackPressedCallback() using OnBackPressedDispatcher() does not work

I opened the dialog fragment by clicking on the recycler view item implemented in the activity.

And for the back function I used onBackPressedCallback().

However, I tried to display a toast message for testing, but nothing happens.

I referred this to the Android developer documentation.

What is the problem?

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

Upvotes: 1

Views: 9724

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199805

As per 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.

You'd see the same issue if you were to put this code in your Activity's onBackPressed() - the Activity is no longer the window receiving key events (of which the system back button is one of them), so it is expected that anything that plugs into that is not triggered.

The only place that receives the call to the system back button when a Dialog is open is the Dialog's onBackPressed() method, which would mean creating your own subclass of Dialog (rather than relying on super.onCreateDialog()) and manually overriding that method.

Of course, if you just want a callback for when the dialog is closed (i.e., the default behavior for the system back button when a dialog is open), then you'd want to override the DialogFragment's onCancel() method.

Upvotes: 4

Related Questions