Skip to content

Commit

Permalink
fix crash for Modal not attached to window manager (2) (#46764)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
alanleedev authored and facebook-github-bot committed Oct 7, 2024
1 parent eb04be8 commit 73ce198
Showing 1 changed file with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ 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
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
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 73ce198

Please sign in to comment.