You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "breautek (via GitHub)" <gi...@apache.org> on 2025/07/30 19:01:16 UTC
[PR] fix(android): Back button override on API 36+ [cordova-android]
breautek opened a new pull request, #1831:
URL: https://github.com/apache/cordova-android/pull/1831
<!--
Please make sure the checklist boxes are all checked before submitting the PR. The checklist is intended as a quick reference, for complete details please see our Contributor Guidelines:
http://cordova.apache.org/contribute/contribute_guidelines.html
Thanks!
-->
### 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. -->
Android API 36 will crash on hardware back button presses due to breaking changes noted [here](https://developer.android.com/about/versions/16/behavior-changes-16#predictive-back)
### Description
<!-- Describe your changes in detail -->
Basically intercepting key events using `onDispatchKeyEvent` no longer works for back button. The return value is ignored for back key events and the system will use the system default action, in addition to whatever code is being triggered by `onDispatchKeyEvent`.
On API 36+, the predictive back feature may take you out of your app, and if it does, the activity may be destroyed. We have key event listeners that will send events to the webview, but in this circumstance the webview will be unusable leading to a crash.
To prevent system behaviour, the OnBackInvokedCallback needs to be registered with the dispatcher. This PR introduces a no-op callback created when the back button is overridden. A reference is kept so that if the app wishes to disable the override later, we can unregister the callback.
There is a comment explaining why the callback is no-op, but to summarize here, attaching the event is all that is needed to prevent default system behaviour. In my testing, the key events are still dispatched just like in earlier API levels, which handles event propagation to the webview... However this may be a bug inside Android considering it's documented that:
> ... Additionally, onBackPressed is not called and [KeyEvent.KEYCODE_BACK](https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_BACK) is not dispatched anymore.
We may need to watch this closely.
### Testing
<!-- Please describe in detail how you tested your changes. -->
Manual testing in-app, as well as npm test passes.
### Checklist
- [x] I've run the tests to see all new and existing tests pass
- [x] I added automated test coverage as appropriate for this change
- [x] Commit is prefixed with `(platform)` if this change only applies to one platform (e.g. `(android)`)
- [x] 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/))
- [x] 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): Back button override on API 36+ [cordova-android]
Posted by "erisu (via GitHub)" <gi...@apache.org>.
erisu merged PR #1831:
URL: https://github.com/apache/cordova-android/pull/1831
--
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): Back button override on API 36+ [cordova-android]
Posted by "lempere (via GitHub)" <gi...@apache.org>.
lempere commented on PR #1831:
URL: https://github.com/apache/cordova-android/pull/1831#issuecomment-3805983172
This is a partial solution based on my testing.
Launching the emulated back button key: `adb shell input keyevent 4` works fine.
However, when you use the back event directly (like the image), it gets captured in the callback and doesn't propagate to the webview.
<img width="398" height="262" alt="image" src="https://github.com/user-attachments/assets/30f9571b-c600-4d1a-9331-0ed6d69c1ce6" />
Adding the `backCallback = () → { webview.backHistory(); };` works, but then the emulated key make two back actions.
--
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): Back button override on API 36+ [cordova-android]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #1831:
URL: https://github.com/apache/cordova-android/pull/1831#issuecomment-3137513646
## [Codecov](https://app.codecov.io/gh/apache/cordova-android/pull/1831?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 72.18%. Comparing base ([`56afb70`](https://app.codecov.io/gh/apache/cordova-android/commit/56afb70894f7443875267a3726f68e9c6ae6caa6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`670e22f`](https://app.codecov.io/gh/apache/cordova-android/commit/670e22f09bc38c9da3ae8e969561d18b011ceae9?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 #1831 +/- ##
==========================================
- Coverage 72.24% 72.18% -0.06%
==========================================
Files 23 23
Lines 1841 1841
==========================================
- Hits 1330 1329 -1
- Misses 511 512 +1
```
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/cordova-android/pull/1831?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): Back button override on API 36+ [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on PR #1831:
URL: https://github.com/apache/cordova-android/pull/1831#issuecomment-3806650908
> Did you try navigating back with the drag edge swipe like in the photo? This back gesture won't tigger any event in the webview.
> <img alt="image" width="398" height="262" src="https://private-user-images.githubusercontent.com/817340/541189045-5df50b8d-6681-490f-a947-3a76bd57aa97.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Njk1MzY4ODAsIm5iZiI6MTc2OTUzNjU4MCwicGF0aCI6Ii84MTczNDAvNTQxMTg5MDQ1LTVkZjUwYjhkLTY2ODEtNDkwZi1hOTQ3LTNhNzZiZDU3YWE5Ny5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwMTI3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDEyN1QxNzU2MjBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0wYjQ5NGU0NjQ2MWNhZWY5YmNkNTlkNTE4MmE0OTY3MjYxM2EyYTkwNjZmOGU4NDI5ZmEzNGI3MWEyMDI4YzBlJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.GfKkDscisJQq0zoZAbw9HG6mJzEvdRzT8_1n7VZvBwQ">
I very likely only tested hardware / software back button, but not gestures. So if gesture back is not working correctly on `nightly` it warrants a new issue I think.
--
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): Back button override on API 36+ [cordova-android]
Posted by "breautek (via GitHub)" <gi...@apache.org>.
breautek commented on PR #1831:
URL: https://github.com/apache/cordova-android/pull/1831#issuecomment-3806119704
> This is a partial solution based on my testing. Launching the emulated back button key: `adb shell input keyevent 4` works fine.
>
> However, when you use the back event directly (like the image), it gets captured in the callback and doesn't propagate to the webview. <img alt="image" width="398" height="262" src="https://private-user-images.githubusercontent.com/817340/541127740-30f9571b-c600-4d1a-9331-0ed6d69c1ce6.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Njk1MzA4NDYsIm5iZiI6MTc2OTUzMDU0NiwicGF0aCI6Ii84MTczNDAvNTQxMTI3NzQwLTMwZjk1NzFiLWM2MDAtNGQxYS05MzMxLTBlZDZkNjljMWNlNi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwMTI3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDEyN1QxNjE1NDZaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT00ODM5MzdhZTk4MDhmYTM1OTlkM2ZjNDE1M2Q4MDcyZDY2OWNhOWYxZjhmMzFmZjQwN2IyMDFmMTExNTA2YmZiJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.3lIFgqHG2JaJmONc4rzaElMgrmNM7_HXY9KFazbSMKc">
>
> Adding the `backCallback = () → { webview.backHistory(); };` works, but then the emulated key make two back actions.
This PR is not in any stable release at the moment, but if you install the `nightly` version of the platform, you should find that the back callback is not fired twice.
--
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): Back button override on API 36+ [cordova-android]
Posted by "uzmakin958-oss (via GitHub)" <gi...@apache.org>.
uzmakin958-oss commented on PR #1831:
URL: https://github.com/apache/cordova-android/pull/1831#issuecomment-3236874515
Open
--
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): Back button override on API 36+ [cordova-android]
Posted by "lempere (via GitHub)" <gi...@apache.org>.
lempere commented on PR #1831:
URL: https://github.com/apache/cordova-android/pull/1831#issuecomment-3806640217
Did you try navigating back with the drag edge swipe like in the photo?
This back gesture won't tigger any event in the webview.
<img width="398" height="262" alt="image" src="https://github.com/user-attachments/assets/5df50b8d-6681-490f-a947-3a76bd57aa97" />
--
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): Back button override on API 36+ [cordova-android]
Posted by "lempere (via GitHub)" <gi...@apache.org>.
lempere commented on PR #1831:
URL: https://github.com/apache/cordova-android/pull/1831#issuecomment-3812833935
I made some more tests and the hardware/software back button neither is dispatched like the gesture back.
<p>
<img src="https://github.com/user-attachments/assets/d55d40f8-2bc4-45aa-ab23-294c3e2cfc22" width="45%" />
<img src="https://github.com/user-attachments/assets/b6e69aa4-26c4-44be-93d1-4142dabd2e42" width="45%" />
</p>
In my tests the emulation back button works fine but not the hardware/software button. That works like in these gif.
--
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