Skip to content

Commit 9a4ef4d

Browse files
authored
Initialize display metrics before reading them in ReactHostImpl.onConfigurationChanged (#60054)
Summary: **Problem:** A configuration change, such as a light/dark mode switch, can crash the app with `IllegalStateException: DisplayMetricsHolder must be initialized` while a `ReactHost` is still starting. **Reproduction:** Start a `ReactHost` without an attached surface, then switch the system dark mode before the `ReactInstance` finishes construction. **Fix:** `ReactHostImpl.onConfigurationChanged` now initializes the display metrics if they are not set yet. It does this before it reads the previous font scale. When the metrics were not set, the old and new font scales match, so no relayout is requested. Behavior after a full start does not change. Changelog: [Android][Fixed] - Fix `IllegalStateException` from `DisplayMetricsHolder` when a configuration change arrives while a `ReactHost` is still starting Reviewed By: rubennorte Differential Revision: D129827368 Pull Request resolved: #60054
1 parent 4a82359 commit 9a4ef4d

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,8 @@ public class ReactHostImpl(
733733
override fun onConfigurationChanged(context: Context) {
734734
val currentReactContext = this.currentReactContext
735735
if (currentReactContext != null) {
736+
// The context is published before ReactInstance initializes the display metrics.
737+
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(currentReactContext)
736738
val previousFontScale = PixelUtil.toPixelFromSP(1.0)
737739
DisplayMetricsHolder.initDisplayMetrics(currentReactContext)
738740
val newFontScale = PixelUtil.toPixelFromSP(1.0)

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactHostTest.kt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
1818
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
1919
import com.facebook.react.internal.featureflags.ReactNativeNewArchitectureFeatureFlagsDefaults
2020
import com.facebook.react.runtime.internal.bolts.Task
21+
import com.facebook.react.uimanager.DisplayMetricsHolder
2122
import com.facebook.react.uimanager.events.BlackHoleEventDispatcher
2223
import com.facebook.testutils.shadows.ShadowSoLoader
2324
import org.assertj.core.api.Assertions.assertThat
@@ -88,6 +89,7 @@ class ReactHostTest {
8889
mockedBridgelessReactContextCtor.close()
8990
mockedMemoryPressureRouterCtor.close()
9091
ReactNativeFeatureFlags.dangerouslyReset()
92+
DisplayMetricsHolder.setScreenDisplayMetrics(null)
9193
}
9294

9395
@Test
@@ -202,4 +204,19 @@ class ReactHostTest {
202204
val newReactContext = mockedBridgelessReactContextCtor.constructed().last()
203205
verify(newReactContext).onHostResume(newActivity.get())
204206
}
207+
208+
@Test
209+
@Suppress("DEPRECATION")
210+
fun onConfigurationChanged_beforeDisplayMetricsAreInitialized_initializesThem() {
211+
DisplayMetricsHolder.setScreenDisplayMetrics(null)
212+
reactHost.start()
213+
val activity = activityController.get()
214+
val reactContext = checkNotNull(reactHost.currentReactContext)
215+
whenever(reactContext.resources).thenReturn(activity.resources)
216+
217+
reactHost.onConfigurationChanged(activity)
218+
219+
assertThat(DisplayMetricsHolder.getScreenDisplayMetrics().scaledDensity)
220+
.isEqualTo(activity.resources.displayMetrics.scaledDensity)
221+
}
205222
}

0 commit comments

Comments
 (0)