Copilot commented on code in PR #1910:
URL: https://github.com/apache/cordova-android/pull/1910#discussion_r3010795810
##########
framework/src/org/apache/cordova/CoreAndroid.java:
##########
@@ -292,15 +293,38 @@ private void registerBackPressedCallback() {
backCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
- // Intentionally empty.
- // On modern Android versions, registering a callback keeps
back handling
- // routed through Cordova's existing key dispatch path.
+ dispatchBackKeyEvent();
}
};
backPressedDispatcherOwner.getOnBackPressedDispatcher().addCallback(backPressedDispatcherOwner,
backCallback);
}
+ private void dispatchBackKeyEvent() {
+ // Build a synthetic BACK key press (DOWN + UP) and route it through
the WebView.
+ // This lets Cordova's existing key handling fire the JS "backbutton"
event.
+ final long eventTime = SystemClock.uptimeMillis();
Review Comment:
The PR description says the fix is to forward `handleOnBackPressed()` to
`getOnBackPressedDispatcher().onBackPressed()` (with the callback temporarily
disabled), but the implementation instead synthesizes BACK key events and only
falls back to the dispatcher when the WebView doesn’t consume them. Please
align the PR description (and/or add a short rationale comment here) so
maintainers understand why synthetic key events are used and when the
dispatcher fallback is expected to run.
##########
framework/src/org/apache/cordova/CoreAndroid.java:
##########
@@ -292,15 +293,38 @@ private void registerBackPressedCallback() {
backCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
- // Intentionally empty.
- // On modern Android versions, registering a callback keeps
back handling
- // routed through Cordova's existing key dispatch path.
+ dispatchBackKeyEvent();
}
};
backPressedDispatcherOwner.getOnBackPressedDispatcher().addCallback(backPressedDispatcherOwner,
backCallback);
}
+ private void dispatchBackKeyEvent() {
+ // Build a synthetic BACK key press (DOWN + UP) and route it through
the WebView.
+ // This lets Cordova's existing key handling fire the JS "backbutton"
event.
+ final long eventTime = SystemClock.uptimeMillis();
+ final KeyEvent downEvent = new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK, 0);
+ final KeyEvent upEvent = new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK, 0);
+
+ final boolean handledDown =
webView.getView().dispatchKeyEvent(downEvent);
+ final boolean handledUp = webView.getView().dispatchKeyEvent(upEvent);
+
+ // If either event was consumed, Cordova/WebView already handled back
behavior.
+ if (handledDown || handledUp) {
+ return;
+ }
+
+ // Otherwise, delegate to Android's default back dispatcher.
+ // Temporarily disable this callback to avoid recursive re-entry.
+ backCallback.setEnabled(false);
+ try {
+ cordova.getActivity().getOnBackPressedDispatcher().onBackPressed();
+ } finally {
Review Comment:
This change alters how overridden back presses (including back gesture
routed via `OnBackPressedDispatcher`) are handled by synthesizing BACK key
events and dispatching them through the WebView. The repo has Android
instrumentation/unit tests, but none appear to cover the
overrideBackbutton/backbutton-JS-event path. Please add an automated test that
registers a JS `backbutton` listener, presses back immediately after app start,
and asserts the JS handler fires (and ideally covers the gesture-backed
dispatcher path as well).
--
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]