fix(android): route Modal hardware ESC through onBackPressedDispatcher so onRequestClose fires#56631
Open
mohanrajvenkatesan2304-hash wants to merge 1 commit intofacebook:mainfrom
Conversation
…r so onRequestClose fires (facebook#56411) Hardware ESC was not invoking JS onRequestClose on Android Modals. Subclass ComponentDialog and override dispatchKeyEvent so KEYCODE_ESCAPE ACTION_UP routes through onBackPressedDispatcher.onBackPressed(), the same path KEYCODE_BACK already uses. Removed the redundant ESC branch from setOnKeyListener to prevent double-dispatch.
77d2138 to
cafb750
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
On Android, pressing the hardware ESC key (external/Bluetooth keyboard) on an open
Modaldid not invoke the JSonRequestClosecallback, even though hardware BACK did. The dialog was being dismissed by a platform path before the JS callback ran, leaving the React tree'svisiblestate stuck.This change:
ReactModalDialogsubclass ofandroidx.activity.ComponentDialogand overridesdispatchKeyEvent.KEYCODE_ESCAPEACTION_UP is forwarded to the dialog's ownonBackPressedDispatcher.onBackPressed(), the same path BACK uses, so both keys now flow through the registeredOnBackPressedCallbackand triggeronRequestClose.KEYCODE_ESCAPEbranch from the modal'ssetOnKeyListenerto avoid double-dispatch.Fixes #56411.
Changelog:
[ANDROID] [FIXED] - Modal
onRequestClosenow fires when the user presses hardware ESC on Android (matching hardware BACK behavior)Test Plan:
ReactModalDialogTest.kt(Robolectric) assertingKEYCODE_ESCAPEACTION_UP routes throughonBackPressedDispatcherand that other key events delegate tosuper.dispatchKeyEvent.yarn test-android. The diff was reviewed for compile correctness against the existingComponentDialogusage on the same file (onBackPressedDispatcheris already used,Theme_FullScreenDialogis defined inthemes.xml).onRequestClosecallback that BACK already produces. Pairing a Bluetooth keyboard and pressing ESC should behave identically.