From 73ce1984e8411a6b4aec440a5e7aa9d2b7474984 Mon Sep 17 00:00:00 2001 From: Alan Lee Date: Mon, 7 Oct 2024 12:31:20 -0700 Subject: [PATCH] fix crash for Modal not attached to window manager (2) (#46764) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46764 This is a patched fix to earlier attempt (D63700769) and resolving crash due to`java.lang.IllegalArgumentException: View=DecorView@b9f88af[AdsManagerActivity] not attached to window manager`. - removing ` !dialogWindow.isActive` check as it is always true resulting in always doing early return causing other bugs - adding try/ catch instead so the code can still run but can catch the known exception without crashing Changelog: [Android][Fixed] - Fix crash for Modal not attached to window manager Reviewed By: mdvacca Differential Revision: D63712422 fbshipit-source-id: 85fb6df340eb1139f954c92b5f1daf0dc41671d2 --- .../react/views/modal/ReactModalHostView.kt | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 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 e784ec3ed17ca3..f61fa85d7b8103 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 @@ -26,6 +26,7 @@ import android.view.WindowManager import android.view.accessibility.AccessibilityEvent import android.widget.FrameLayout import androidx.annotation.UiThread +import com.facebook.common.logging.FLog import com.facebook.react.R import com.facebook.react.bridge.GuardedRunnable import com.facebook.react.bridge.LifecycleEventListener @@ -33,6 +34,7 @@ import com.facebook.react.bridge.ReactContext import com.facebook.react.bridge.UiThreadUtil import com.facebook.react.bridge.WritableMap import com.facebook.react.bridge.WritableNativeMap +import com.facebook.react.common.ReactConstants import com.facebook.react.common.annotations.VisibleForTesting import com.facebook.react.config.ReactFeatureFlags import com.facebook.react.uimanager.JSPointerDispatcher @@ -306,29 +308,37 @@ public class ReactModalHostView(context: ThemedReactContext) : val dialogWindow = checkNotNull(dialog.window) { "dialog must have window when we call updateProperties" } val currentActivity = getCurrentActivity() - if (currentActivity == null || currentActivity.isFinishing || !dialogWindow.isActive) { + if (currentActivity == null || currentActivity.isFinishing || currentActivity.isDestroyed) { // If the activity has disappeared, then we shouldn't update the window associated to the // Dialog. return } - val activityWindow = currentActivity.window - if (activityWindow != null) { - val activityWindowFlags = activityWindow.attributes.flags - if ((activityWindowFlags and WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0) { - dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) - } else { - dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) + try { + val activityWindow = currentActivity.window + if (activityWindow != null) { + val activityWindowFlags = activityWindow.attributes.flags + if ((activityWindowFlags and WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0) { + dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) + } else { + dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) + } } - } - dialogWindow.setStatusBarTranslucency(statusBarTranslucent) + dialogWindow.setStatusBarTranslucency(statusBarTranslucent) - if (transparent) { - dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) - } else { - dialogWindow.setDimAmount(0.5f) - dialogWindow.setFlags( - WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND) + if (transparent) { + dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) + } else { + dialogWindow.setDimAmount(0.5f) + dialogWindow.setFlags( + WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND) + } + } catch (e: IllegalArgumentException) { + // This is to prevent a crash from the following error, without a clear repro steps: + // java.lang.IllegalArgumentException: View=DecorView@c94931b[XxxActivity] not attached to + // window manager + FLog.e( + ReactConstants.TAG, "ReactModalHostView: error while setting window flags: ", e.message) } }