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


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

Review Comment:
   I believe this cast is also redundent
   
   
   ```suggestion
           final OnBackPressedDispatcherOwner backPressedDispatcherOwner = 
this.cordova.getActivity();
   ```
   
   Because `AppCompatActivity` implements the `OnBackPressedDispatcherOwner` 
interface via extending `ComponentActivity`, so the object should simply be 
assignable without doing an explicit cast.



-- 
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