From 5ca5ef4b0c153063232d6d0c1af09d5aacec91b3 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Sun, 1 Nov 2026 08:53:33 +0000 Subject: [PATCH 1/3] fix(mobile): keep Android split screen from restarting or squeezing the app One UI gives a split-screen pane its own density. Android recreated MainActivity on that change, and React Native seeds its display metrics from the application context, which keeps the full-screen density, so the pane was laid out at the wrong scale. A config plugin declares density in configChanges and re-seeds the metrics from the activity. --- mobile/app.json | 1 + mobile/plugins/android-window-density.js | 93 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 mobile/plugins/android-window-density.js diff --git a/mobile/app.json b/mobile/app.json index 17e1d162f3dc..8196e3d1da0c 100644 --- a/mobile/app.json +++ b/mobile/app.json @@ -110,6 +110,7 @@ ], "expo-notifications", "./plugins/android-respect-rotation-lock.js", + "./plugins/android-window-density.js", "./plugins/ios-pod-deployment-target-floor.js", "./plugins/ios-scene-lifecycle.js", [ diff --git a/mobile/plugins/android-window-density.js b/mobile/plugins/android-window-density.js new file mode 100644 index 000000000000..c06c6a3b5a19 --- /dev/null +++ b/mobile/plugins/android-window-density.js @@ -0,0 +1,93 @@ +const { AndroidConfig, withAndroidManifest, withMainActivity } = require('expo/config-plugins') + +// Why: One UI gives a split-screen pane its own density (#36305). Without `density` in +// configChanges Android recreates MainActivity, closing the open session and orphaning the image +// picker. Keeping the activity is not enough: React Native seeds its DisplayMetricsHolder from the +// application context, which keeps the full-screen density, so the pane is laid out at the wrong +// scale. MainActivity re-seeds it from its own resources after React Native does. +const MARKER = 'orca: window density' + +const CLASS_HEADER = 'class MainActivity : ReactActivity() {' +const IMPORTS = [ + 'android.content.res.Configuration', + 'android.view.ViewTreeObserver', + 'com.facebook.react.uimanager.DisplayMetricsHolder' +] + +const METHODS = ` + // ${MARKER}, generated by plugins/android-window-density.js. + // React Native re-seeds from the application context when it creates the root view (onCreate), + // in onConfigurationChanged, and in its root view's layout listener after a rotation. + override fun onResume() { + super.onResume() + useWindowDensity() + } + + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + useWindowDensity() + // Added after the root view's listener, so this runs after its rotation re-seed. + val observer = window.decorView.viewTreeObserver + observer.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { + override fun onGlobalLayout() { + observer.removeOnGlobalLayoutListener(this) + val held = DisplayMetricsHolder.getScreenDisplayMetrics().densityDpi + if (held != resources.displayMetrics.densityDpi) { + useWindowDensity() + window.decorView.requestLayout() + } + } + }) + } + + private fun useWindowDensity() { + DisplayMetricsHolder.initDisplayMetrics(this) + val pane = resources.displayMetrics + DisplayMetricsHolder.getScreenDisplayMetrics().apply { + density = pane.density + densityDpi = pane.densityDpi + } + } +` + +function patchMainActivity(contents) { + if (contents.includes(MARKER)) { + return contents + } + // A changed template must fail prebuild rather than drop or double an override. + if ( + !contents.includes(CLASS_HEADER) || + !contents.trimEnd().endsWith('}') || + /fun (onResume|onConfigurationChanged)\(/.test(contents) + ) { + throw new Error( + "android-window-density: Expo's MainActivity template changed; update the plugin before building." + ) + } + const imports = IMPORTS.filter((name) => !contents.includes(`import ${name}\n`)) + .map((name) => `import ${name}\n`) + .join('') + const body = contents.trimEnd().slice(0, -1).trimEnd() + return `${body.replace(/^(package [^\n]+\n)/, `$1${imports}`)}\n${METHODS}}\n` +} + +module.exports = function withAndroidWindowDensity(config) { + config = withAndroidManifest(config, (cfg) => { + const activity = AndroidConfig.Manifest.getMainActivityOrThrow(cfg.modResults) + const changes = (activity.$['android:configChanges'] ?? '').split('|').filter(Boolean) + if (!changes.includes('density')) { + activity.$['android:configChanges'] = [...changes, 'density'].join('|') + } + return cfg + }) + + return withMainActivity(config, (cfg) => { + if (cfg.modResults.language !== 'kt') { + throw new Error( + `android-window-density: expected a Kotlin MainActivity, got ${cfg.modResults.language}` + ) + } + cfg.modResults.contents = patchMainActivity(cfg.modResults.contents) + return cfg + }) +} From 87d94fad5f05665c560a557d32007547384171b0 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Sun, 1 Nov 2026 09:23:24 +0000 Subject: [PATCH 2/3] fix(mobile): catch React Native's rotation re-seed before the frame draws A one-shot listener added on configuration change missed a cold start or a 180-degree flip at a non-zero rotation, where React Native's root view re-seeds from the application context without a configuration change. --- mobile/plugins/android-window-density.js | 34 +++++++++++++----------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/mobile/plugins/android-window-density.js b/mobile/plugins/android-window-density.js index c06c6a3b5a19..602b03931c10 100644 --- a/mobile/plugins/android-window-density.js +++ b/mobile/plugins/android-window-density.js @@ -10,14 +10,28 @@ const MARKER = 'orca: window density' const CLASS_HEADER = 'class MainActivity : ReactActivity() {' const IMPORTS = [ 'android.content.res.Configuration', - 'android.view.ViewTreeObserver', + 'android.os.Bundle', 'com.facebook.react.uimanager.DisplayMetricsHolder' ] const METHODS = ` // ${MARKER}, generated by plugins/android-window-density.js. - // React Native re-seeds from the application context when it creates the root view (onCreate), - // in onConfigurationChanged, and in its root view's layout listener after a rotation. + // React Native re-seeds from the application context when it creates the root view (onCreate) + // and in onConfigurationChanged, so these run before the next measure. Its root view's layout + // listener also re-seeds whenever the rotation differs from the last one it saw, after measuring; + // the pre-draw check runs after every layout listener and lays out again. + override fun onPostCreate(savedInstanceState: Bundle?) { + super.onPostCreate(savedInstanceState) + window.decorView.viewTreeObserver.addOnPreDrawListener { + val held = DisplayMetricsHolder.getScreenDisplayMetrics().densityDpi + if (held != resources.displayMetrics.densityDpi) { + useWindowDensity() + window.decorView.requestLayout() + } + true + } + } + override fun onResume() { super.onResume() useWindowDensity() @@ -26,18 +40,6 @@ const METHODS = ` override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) useWindowDensity() - // Added after the root view's listener, so this runs after its rotation re-seed. - val observer = window.decorView.viewTreeObserver - observer.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { - override fun onGlobalLayout() { - observer.removeOnGlobalLayoutListener(this) - val held = DisplayMetricsHolder.getScreenDisplayMetrics().densityDpi - if (held != resources.displayMetrics.densityDpi) { - useWindowDensity() - window.decorView.requestLayout() - } - } - }) } private fun useWindowDensity() { @@ -58,7 +60,7 @@ function patchMainActivity(contents) { if ( !contents.includes(CLASS_HEADER) || !contents.trimEnd().endsWith('}') || - /fun (onResume|onConfigurationChanged)\(/.test(contents) + /fun (onPostCreate|onResume|onConfigurationChanged)\(/.test(contents) ) { throw new Error( "android-window-density: Expo's MainActivity template changed; update the plugin before building." From 94c04dcc08bd0f7d3cbf080c250948a8cbe53673 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Sun, 1 Nov 2026 09:47:22 +0000 Subject: [PATCH 3/3] fix(mobile): publish re-seeded display metrics in one step Text measurement reads the holder off the UI thread, so build the pane-density screen metrics before publishing them. --- mobile/plugins/android-window-density.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mobile/plugins/android-window-density.js b/mobile/plugins/android-window-density.js index 602b03931c10..d2c7a9d7d8b4 100644 --- a/mobile/plugins/android-window-density.js +++ b/mobile/plugins/android-window-density.js @@ -11,6 +11,7 @@ const CLASS_HEADER = 'class MainActivity : ReactActivity() {' const IMPORTS = [ 'android.content.res.Configuration', 'android.os.Bundle', + 'android.util.DisplayMetrics', 'com.facebook.react.uimanager.DisplayMetricsHolder' ] @@ -19,7 +20,8 @@ const METHODS = ` // React Native re-seeds from the application context when it creates the root view (onCreate) // and in onConfigurationChanged, so these run before the next measure. Its root view's layout // listener also re-seeds whenever the rotation differs from the last one it saw, after measuring; - // the pre-draw check runs after every layout listener and lays out again. + // the pre-draw check runs after every layout listener and asks for another pass, in which React + // Native re-sends the window size to JS. override fun onPostCreate(savedInstanceState: Bundle?) { super.onPostCreate(savedInstanceState) window.decorView.viewTreeObserver.addOnPreDrawListener { @@ -42,13 +44,19 @@ const METHODS = ` useWindowDensity() } + // React Native's initDisplayMetrics with the pane's density on the real display size, built before + // it is published because text measurement reads it off the UI thread. + @Suppress("DEPRECATION") private fun useWindowDensity() { - DisplayMetricsHolder.initDisplayMetrics(this) val pane = resources.displayMetrics - DisplayMetricsHolder.getScreenDisplayMetrics().apply { + val screen = DisplayMetrics().apply { + windowManager.defaultDisplay.getRealMetrics(this) density = pane.density densityDpi = pane.densityDpi + scaledDensity = pane.scaledDensity } + DisplayMetricsHolder.setWindowDisplayMetrics(pane) + DisplayMetricsHolder.setScreenDisplayMetrics(screen) } `