mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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
This commit is contained in:
+26
-16
@@ -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) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user