fix(android): intercept ESCAPE key ACTION_DOWN in Modal to prevent premature dismiss#56498
Open
taibin wants to merge 1 commit intofacebook:mainfrom
Open
fix(android): intercept ESCAPE key ACTION_DOWN in Modal to prevent premature dismiss#56498taibin wants to merge 1 commit intofacebook:mainfrom
taibin wants to merge 1 commit intofacebook:mainfrom
Conversation
…emature dismiss The Modal's OnKeyListener previously only handled BACK/ESCAPE on ACTION_UP, but Dialog.onKeyDown() directly calls cancel() for KEYCODE_ESCAPE on ACTION_DOWN (unlike KEYCODE_BACK, which defers to onBackPressed on ACTION_UP). As a result, ESCAPE closed the dialog before JS was ever notified. Restructure the listener to branch on keyCode first: - For BACK/ESCAPE: consume ACTION_DOWN to block Dialog's default handling, then call handleCloseAction() on ACTION_UP so JS can decide whether to close. - For all other keys: keep the existing behavior of forwarding ACTION_UP to the current Activity (needed by the dev menu, etc.).
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
Fix Android Modal's handling of the ESCAPE key so that JavaScript is reliably notified (via
onRequestClose) instead of the dialog dismissing itself before JS sees the event.Problem
ReactModalHostViewinstalls anOnKeyListenerthat only reacts onACTION_UPforKEYCODE_BACK/KEYCODE_ESCAPE, callinghandleCloseAction()to let JS decide whether to close.However, Android's
Dialog.onKeyDown()handles these two keys asymmetrically:KEYCODE_BACK—ACTION_DOWNonly callsevent.startTracking(); the dialog is dismissed onACTION_UPviaonBackPressed(). OurACTION_UPinterception works fine.KEYCODE_ESCAPE—ACTION_DOWNdirectly callsDialog.cancel(). By the time ourACTION_UPhandler would run, the dialog is already being dismissed, soonRequestCloseis never delivered to JS.Net effect: pressing ESCAPE on Android closes the Modal unconditionally, bypassing JS (breaks apps that rely on
onRequestClosefor confirmation dialogs, unsaved-changes prompts, etc.).Fix
Restructure the key listener to branch on
keyCodefirst:KEYCODE_BACK/KEYCODE_ESCAPE:ACTION_DOWN(return true) to preventDialog.onKeyDown()from dismissing the dialog.ACTION_UP, callhandleCloseAction()as before, giving JS the final say.ACTION_UPto the currentActivity(needed by the dev menu, etc.).Test Plan
<Modal visible>on Android, attachonRequestClosehandler, press the hardware BACK button — handler fires, modal stays open unless JS setsvisible={false}. (Regression coverage: unchanged behavior.)Changelog
[ANDROID] [FIXED] - Modal's
onRequestCloseis now correctly invoked when pressing ESCAPE on Android (previously the dialog was dismissed before JS was notified).