From ad787aad314fc1963678d4677a7a3d71af0f1c48 Mon Sep 17 00:00:00 2001 From: Autopilot Bot Date: Tue, 20 Oct 2026 08:58:17 -0700 Subject: [PATCH] Avoid crash dismissing a Modal whose Activity was destroyed Summary: `ReactModalHostView.dismiss()` skipped `Dialog.dismiss()` only when the dialog's Activity was finishing. When that Activity is destroyed without finishing (for example relaunched on a configuration change while a Modal is open), its windows are already removed, so `Dialog.dismiss()` throws `IllegalArgumentException: View=DecorView[...] not attached to window manager` and crashes the app during teardown. Also skip the dismiss when the Activity `isDestroyed`, and catch `IllegalArgumentException` around `Dialog.dismiss()`, matching the existing guard in `updateProperties()`. Changelog: [Android][Fixed] - Fix crash when dismissing a Modal whose Activity has already been destroyed Differential Revision: D125800172 --- .../facebook/react/views/modal/ReactModalHostView.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt index f0415bf23dd1..0cd7342ccb7e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt @@ -204,8 +204,14 @@ public class ReactModalHostView(context: ThemedReactContext) : } val dialogContext = ContextUtils.findContextOfType(nonNullDialog.context, Activity::class.java) - if (dialogContext == null || !dialogContext.isFinishing) { - nonNullDialog.dismiss() + // A destroyed Activity (e.g. relaunched on a configuration change without finishing) has + // already removed the dialog's window, so dismissing it would throw. + if (dialogContext == null || !(dialogContext.isFinishing || dialogContext.isDestroyed)) { + try { + nonNullDialog.dismiss() + } catch (e: IllegalArgumentException) { + FLog.e(TAG, "ReactModalHostView: error while dismissing dialog: ", e.message) + } } } dialog = null