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: [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]
