Skip to content

fix(android): Back button override on API 36+ #1831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

breautek
Copy link
Contributor

@breautek breautek commented Jul 30, 2025

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 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 is not dispatched anymore.

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

  • 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)
  • I've updated the documentation if necessary

@breautek breautek added this to the 15.0.0 milestone Jul 30, 2025
@codecov-commenter
Copy link

codecov-commenter commented Jul 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.24%. Comparing base (56afb70) to head (670e22f).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1831   +/-   ##
=======================================
  Coverage   72.24%   72.24%           
=======================================
  Files          23       23           
  Lines        1841     1841           
=======================================
  Hits         1330     1330           
  Misses        511      511           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants