You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "GitToTheHub (via GitHub)" <gi...@apache.org> on 2026/03/13 15:55:59 UTC
[PR] fix(android): avoid `OnBackInvokedCallback` class-load crashes on older runtimes in CoreAndroid [cordova-android]
GitToTheHub opened a new pull request, #1903:
URL: https://github.com/apache/cordova-android/pull/1903
### Platforms affected
Android
### Motivation and Context
<!-- Why is this change required? What problem does it solve? -->
<!-- If it fixes an open issue, please link to the issue here. -->
- Fixes error message: `java.lang.Class<org.apache.cordova.CoreAndroid$$ExternalSyntheticLambda0>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/window/OnBackInvokedCallback;` on older Android versions pre API Level 33 (Android 13)
- The use of `OnBackInvokedCallback` and `OnBackInvokedDispatcher` was introduced with PR https://github.com/apache/cordova-android/pull/1831/changes
- This change fixes startup log spam and plugin instantiation failures on older Android runtimes where `android.window.OnBackInvokedCallback` is unavailable, because it is introduced since API Level 33 (Android 13) and not available before.
- `CoreAndroid.java` referenced `OnBackInvokedCallback` directly (including lambda generation), which can trigger class resolution on runtimes that do not contain that API.
- Fix: replaced direct API typing with reflection + dynamic proxy registration/unregistration of predictive-back callback.
- Behavior: predictive-back callback is still registered when supported; on unsupported runtimes, registration is skipped safely and app startup continues without `NoClassDefFoundError` noise.
- Generated-By: GPT-5.3-Codex, GitHub Copilot Chat
### Description
<!-- Describe your changes in detail -->
### Testing
<!-- Please describe in detail how you tested your changes. -->
### Checklist
- [ ] I've run the tests to see all new and existing tests pass
- [ ] I added automated test coverage as appropriate for this change
- [ ] Commit is prefixed with `(platform)` if this change only applies to one platform (e.g. `(android)`)
- [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/))
- [ ] I've updated the documentation if necessary
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4373563871
Hi, I'm glad to hear that it works now for you
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2933012874
##########
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:
Yah -- I know the android docs states that events are blocked/ignored but that doesn't seem to be the case in practice...
It means we might not need to do implement or run `redispatchBackKeyEvent`...
In my original PR -- that's why I had an empty back pressed callback being attached.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932873397
##########
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:
Ok I will test this
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932951048
##########
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:
the overrideBackButton method gets called if the user binds the `backbutton` event on the window document...
So after cordova loaded if the JS does:
```
document.addEventListener('backbutton', function() { ... });
```
It should trigger the cordova bridge call which calls on the native `overrideBackButton` method.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932787385
##########
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:
Yes I fixed that already
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4057171455
Good :)
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub merged PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2933012874
##########
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:
Yah -- I know the android docs states that events are blocked/ignored but that doesn't seem to be the case in practice...
It means we might not need to do implement or run `redispatchBackKeyEvent`...
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2933012874
##########
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:
Yah -- I know the android docs states that events are blocked/ignored but that doesn't seem to be the case in practice...
It means we might not need to do implement or run `redispatchBackKeyEvent`...
In my original PR -- that's why I had an empty back pressed callback being attached.
```
// 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.
```
So basically the old way is a boolean return controlled whether the system acted on the back button for some default behaviour. Now that boolean return is ignored, and if we want the system to not conduct default behaviour on the back button, the callback needs to be attached. But the callback shouldn't really do anything special.
I probably tested earlier API versions as well to ensure behaviour was consisted, I probably just didn't test pre-33 and thus didn't catch the runtime error on using API 33+ APIs...
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4296379019
Hi currently there is no scheduled release date. I think currently we have not a good point to make release, since there are some things around the statusbar and edge-to-edge
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4351187220
Can you describe more what you did? Did you tried the latest master of this plugin? Which Android version did you test on? Did you tried it on an emulator or a real device? This PR here does not fix all back button problems.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): avoid `OnBackInvokedCallback` class-load crashes on older runtimes [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4056390611
If there is a compat library, this would be better. I will try to use the appcompat library.
I don't know, how an import could be optional by a SDK int check.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932868946
##########
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:
Creating a hello world app that logs out when the `backbutton` event fires should be sufficient.
The expectation is one event per back button press.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932997719
##########
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:
I see the back button log message twice when pressing the back button
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2933047900
##########
templates/project/build/reports/problems/problems-report.html:
##########
Review Comment:
Probably don't need to commit this file (and perhaps in another PR we can gitignore `templates/project/build`
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): avoid `OnBackInvokedCallback` class-load crashes on older runtimes [cordova-android]
Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932744283
##########
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:
## Useless type test
There is no need to test whether an instance of [AppCompatActivity](1) is also an instance of [OnBackPressedDispatcherOwner](2) - it always is.
[Show more details](https://github.com/apache/cordova-android/security/code-scanning/274)
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "MoekoNakajima (via GitHub)" <gi...@apache.org>.
MoekoNakajima commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4349177694
I tried applying this fix, but the Back button didn't work.
Are you considering any additional fixes?
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932852505
##########
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:
How can this be tested?
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2933012874
##########
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:
Yah -- I know the android docs states that events are blocked/ignored but that doesn't seem to be the case in practice...
It means we might not need to do implement or run `redispatchBackKeyEvent`...
In my original PR -- that's why I had an empty back pressed callback being attached.
```
// 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.
```
So basically the old way is a boolean return controlled whether the system acted on the back button for some default behaviour. Now that boolean return is ignored, and if we want the system to not conduct default behaviour on the back button, the callback needs to be attached. But the callback shouldn't really do anything special.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "MoekoNakajima (via GitHub)" <gi...@apache.org>.
MoekoNakajima commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4357501686
>Did you tried the latest master of cordova-android?
> The PR https://github.com/apache/cordova-android/pull/1910 should fix back button issues.
I had updated to Cordova Android 15.0.0, but I hadn't tried the latest master branch yet.
I have now tested the latest master branch (which includes PR #1910 ) and confirmed that the back button works properly.
Thank you for pointing that out.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "MoekoNakajima (via GitHub)" <gi...@apache.org>.
MoekoNakajima commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4309849457
Thank you for letting me know. I understand the situation. I am looking forward to the release!
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): avoid `OnBackInvokedCallback` class-load crashes on older runtimes [cordova-android]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4056193515
## [Codecov](https://app.codecov.io/gh/apache/cordova-android/pull/1903?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 61.43%. Comparing base ([`c02d1b0`](https://app.codecov.io/gh/apache/cordova-android/commit/c02d1b0331d82c0480a275804f85d134ad201155?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`93d10a9`](https://app.codecov.io/gh/apache/cordova-android/commit/93d10a9149c59a32a0ac4d0f8608f4c37c493ee1?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #1903 +/- ##
=======================================
Coverage 61.43% 61.43%
=======================================
Files 24 24
Lines 4922 4922
=======================================
Hits 3024 3024
Misses 1898 1898
```
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/cordova-android/pull/1903?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): avoid `OnBackInvokedCallback` class-load crashes on older runtimes [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4056250416
I think the proper fix is to actually use the androidx appcompat library: https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher
but testing will be required to ensure it has the expected behaviour.
I don't like using reflections because it makes the code harder to maintain, hide deprecation warnings, can prevent JIT optimzations and so on... In my personal experience I never really found a reason to use reflection over another solution.
If the androidx is not sufficient, then it would be better to do an SDK int check to ensure we don't run API 33+ code on pre-33 devices, as the code should be compilable just not runnable, for as long as compileSdkVersion is set to 33+.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932868946
##########
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:
Creating a hello world app that consle.log out when the `backbutton` event fires should be sufficient.
The expectation is one event per back button press.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "GitToTheHub (via GitHub)" <gi...@apache.org>.
GitToTheHub commented on code in PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#discussion_r2932913896
##########
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:
Does the overriding only occur on Android 36? Should I see the log message `WARNING: Back Button Default Behavior will be overridden. The backbutton event will be fired!` when this happens?
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org
Re: [PR] fix(android): use AndroidX `OnBackPressedCallback` instead of `OnBackInvokedCallback` [cordova-android]
Posted by "MoekoNakajima (via GitHub)" <gi...@apache.org>.
MoekoNakajima commented on PR #1903:
URL: https://github.com/apache/cordova-android/pull/1903#issuecomment-4292991121
Could I ask you a question?
Is there a scheduled release date for this update?
If so, I would appreciate it if you could let me know.
--
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: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org