fix(android): Back button override on API 36+ #1831
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Platforms affected
Android
Motivation and Context
Android API 36 will crash on hardware back button presses due to breaking changes noted here
Description
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 byonDispatchKeyEvent
.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:
We may need to watch this closely.
Testing
Manual testing in-app on API 36 Google Play simulator, as well as npm test passes.
Checklist
(platform)
if this change only applies to one platform (e.g.(android)
)