[Android] Can you tell me the difference between onBackPressed and onCancel?

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

After implementing the dialog fragment, you want to implement the Back button in the displayed window.

If you press the onBackPressed button, it doesn't work.Or onBackPressedCallback()

In the case of DialogFragment, it occurs above window of Activity, so it intercepts the backkey event at the top. I don't know exactly what you mean

Why the implementation of onBackPressed (or onBackPressedCallBack does not work when the dialog intercepts the backkey event

I don't know.

So, if you want to implement back, why do you want to implement onCancel?

What's the difference between these two?

android dialog fragment

2022-09-20 15:55

1 Answers

Hello. Looking at it, I think there was a confusion in the answer I gave you before. I don't think the explanation was enough :)

Android has a Window class, which is an internal class, so it is not accessible, but exactly a class called PhoneWindow is utilized. This Phone Window has a DecorView used as the top-level parent View and a WindowManager class to manage the window.

Activity is created within each window and Dialog is also created within the window. Therefore, the UI of the application can be seen as a structure with layers of windows.

I think there is room for confusion in the expression "Dialog intercepts the backkey event". To be precise, KeyEvent is delivered to the Window first, like TouchEvent, and then Activity (or dialog) -> View along the UI layer. In fact, there are APIs such as onInterceptTouchEvent() to intercept events in the middle.

To get to the point, if a backup key event occurs while a dialog occurs, the window that configures the dialog receives the event.

In addition, onBackPressedCallBack is called through onBackPressedDispatcher in Activity. However, onBackPressedCallback is not available because Dialog is already occurring in a new window above Activity and this window receives a KeyEvent first.

I mentioned onCancel and onDismiss in the last answer without knowing exactly what scenario you want to implement, but in the case of this callback, it is a callback that is invoked when the Dialog disappears, so it seems inappropriate to answer the above question.

There are two ways to think about it.

The first is to use the Dialog class onBackPressed() because the Fragment class does not provide onBackPressed(). Implement onBackPressed() when generating the Dialog in onCreateDialog.

 override fun onCreateDialog (savedInstanceState: Bundle?): Dialog {
    return object : Dialog(requireContext()) {
        override fun onBackPressed() {
            //TODO: Add code required for backup key event

The second method is to register the KeyListener from a Dialog object that has already been created and then process the Backkey event.

override fun onViewCreated(view: savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setOnKeyListener { dialog, keyCode, event ->
            if(keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
                //TODO: Add code required for backup key event
                [email protected] true
            [email protected] false

But Dialog.If you look at the java code, the call of onBackPressed() is already implemented through KeyListener, so please note the differences in the internal code and use it.

2022-09-20 15:55

If you have any answers or tips

© 2022 pinfo. All rights reserved.