breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932782318
##########
framework/src/org/apache/cordova/CoreAndroid.java:
##########
@@ -253,32 +254,61 @@
*/
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() {
+ if (!(this.cordova.getActivity() instanceof
OnBackPressedDispatcherOwner)) {
Review Comment:
Probably worth applying this CodeQL suggestion.
`cordova.getActivity()` does always returns an AppCompatActivity, which via
extension of ComponentActivity is also a `OnBackPressedDispatcherOwner` so
there is no reason to do an explicit cast either.
--
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]