breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932846595


##########
framework/src/org/apache/cordova/CoreAndroid.java:
##########
@@ -253,32 +254,56 @@ public void run() {
      */
     public void overrideBackbutton(boolean override) {
         LOG.i("App", "WARNING: Back Button Default Behavior will be 
overridden.  The backbutton event will be fired!");
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
-            if (override) {
-                synchronized (backButtonHandlerLock) {
-                    if (backCallback == null) {
-                        // The callback is intentionally empty. Since API 36, 
intercepting the back button is ignored, which means
-                        // the onDispatchKeyEvent boolean result won't 
actually stop native from consuming the back button and doing
-                        // it's own logic, UNLESS if there is an 
OnBackInvokedCallback registered on the dispatcher.
-                        // The key dispatch events will still fire, which 
still handles propagating back button events to the webview.
-                        // See 
https://developer.android.com/about/versions/16/behavior-changes-16#predictive-back
 for more info on the caveat.
-                        backCallback = () -> {};
-                        
this.cordova.getActivity().getOnBackInvokedDispatcher().registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT,
 backCallback);
-                    }
+        if (override) {
+            synchronized (backButtonHandlerLock) {
+                if (backCallback == null) {
+                    registerBackPressedCallback();
                 }
-            } else {
-                synchronized (backButtonHandlerLock) {
-                    if (backCallback != null) {
-                        
this.cordova.getActivity().getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(backCallback);
-                        backCallback = null;
-                    }
+            }
+        } else {
+            synchronized (backButtonHandlerLock) {
+                if (backCallback != null) {
+                    unregisterBackPressedCallback();
                 }
             }
         }
 
         webView.setButtonPlumbedToJs(KeyEvent.KEYCODE_BACK, override);
     }
 
+    /**
+     * Registers an AndroidX back callback so Cordova can keep routing back 
presses through its
+     * existing key dispatch path across Android versions without directly 
referencing newer
+     * platform-only back APIs.
+     */
+    private void registerBackPressedCallback() {
+        final OnBackPressedDispatcherOwner backPressedDispatcherOwner =
+                (OnBackPressedDispatcherOwner) this.cordova.getActivity();
+
+        backCallback = new OnBackPressedCallback(true) {
+            @Override
+            public void handleOnBackPressed() {
+                redispatchBackKeyEvent();
+            }
+        };
+
+        
backPressedDispatcherOwner.getOnBackPressedDispatcher().addCallback(backPressedDispatcherOwner,
 backCallback);
+    }
+
+    private void unregisterBackPressedCallback() {
+        backCallback.remove();
+        backCallback = null;
+    }
+
+    private void redispatchBackKeyEvent() {
+        if (webView == null || webView.getView() == null) {
+            return;
+        }
+
+        webView.getView().dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, 
KeyEvent.KEYCODE_BACK));
+        webView.getView().dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, 
KeyEvent.KEYCODE_BACK));
+    }
+

Review Comment:
   When I implemented the original PR... despite documentation I found that the 
back button still propagated to the webview without doing anything extra... so 
I didn't need to dispatch any key events... and doing so resulted the back 
button to double-fire, so the back button events fired twice.
   
   Idk if that was because I was using the dispatcher API directly instead of 
going through the AndroidX compatibility layer... but it's worth 
double-checking that we are not introducing that issue.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to