Repository navigation
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.).
|
Warning JavaScript API change detected This PR commits an update to
This change was flagged as: |
|
This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
|
Confirmed on React Native 0.88.0 (New Architecture, Expo SDK 58), Android 17 emulator, debug build: with a The cause is the one this PR describes: For comparison, #56631 forwards only ESCAPE's ACTION_UP and, as its own test asserts, lets ACTION_DOWN through to |
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).