Skip to content

fix: overwritten backbutton not firing on first time by hardware button and never on back gesture#1910

Open
GitToTheHub wants to merge 4 commits intomasterfrom
backbutton-fix
Open

fix: overwritten backbutton not firing on first time by hardware button and never on back gesture#1910
GitToTheHub wants to merge 4 commits intomasterfrom
backbutton-fix

Conversation

@GitToTheHub
Copy link
Copy Markdown
Contributor

@GitToTheHub GitToTheHub commented Mar 30, 2026

Platforms affected

Android

Motivation and Context

  • When pressing the back button after the app starts nothing happens. When navigating to another page, the back button js event starts to fire.
  • On Android versions, which support the back gesture, nothing happens
  • This was due to the empty OnBackPressedCallback.handleOnBackPressed method, which has to forward its call to the WebView, which is done now by a synthetic back button event. If the WebView does not handle the synthetic back button event, the back button if forwarded to cordova.getActivity().getOnBackPressedDispatcher().onBackPressed().
  • Generated-By: GPT-5.4, GitHub Copilot Chat

Code cleanup

  • Put log warning about back button override on the right place
  • resolve methods registerBackPressedCallback and unregisterBackPressedCallback. The code moved to inline code.

Description

Testing

  • Tested back button after first start on Android 9
  • Test back button gesture on Android 14 and 16

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

…k gesture

- When pressing the back button after the app starts nothing happens. When navigating to another page, the back button js event starts to fire.
- On Android versions, which support the back gesture, nothing happens
- This was due to the empty `OnBackPressedCallback.handleOnBackPressed` method, which has to forward its call to `cordova.getActivity().getOnBackPressedDispatcher().onBackPressed()`, but has to disable during this call the `backCallback`.
- Generated-By: GPT-5.4, GitHub Copilot Chat
Copilot AI review requested due to automatic review settings March 30, 2026 16:06
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 61.43%. Comparing base (1f70d39) to head (9733d15).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1910   +/-   ##
=======================================
  Coverage   61.43%   61.43%           
=======================================
  Files          24       24           
  Lines        4922     4922           
=======================================
  Hits         3024     3024           
  Misses       1898     1898           

☔ 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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Android back handling when the Cordova back button is overridden, ensuring the JS backbutton event fires immediately after startup and also when back is triggered via the Android back gesture (OnBackPressedDispatcher path).

Changes:

  • Implemented OnBackPressedCallback.handleOnBackPressed() to actively dispatch a synthetic BACK key (DOWN+UP) through the Cordova WebView so existing key routing can fire the JS backbutton event.
  • Added fallback to OnBackPressedDispatcher.onBackPressed() when the WebView does not consume the synthetic key events, with temporary callback disabling to prevent recursion.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread framework/src/org/apache/cordova/CoreAndroid.java Outdated
Comment thread framework/src/org/apache/cordova/CoreAndroid.java Outdated
@GitToTheHub GitToTheHub changed the title fix: overwritten backbutton not firing on first time and never on back gesture fix: overwritten backbutton not firing on first time by hardware and never on back gesture Mar 30, 2026
@GitToTheHub GitToTheHub changed the title fix: overwritten backbutton not firing on first time by hardware and never on back gesture fix: overwritten backbutton not firing on first time by hardware button and never on back gesture Mar 30, 2026
@GitToTheHub
Copy link
Copy Markdown
Contributor Author

Hi @breautek, what are your thoughts about this PR? Is this ready to merge?

@GitToTheHub
Copy link
Copy Markdown
Contributor Author

I made some code cleanup

Copy link
Copy Markdown
Contributor

@breautek breautek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any beefs code wise. I'm trusting community feedback on functionality.

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.

4 participants