Skip to content

Commit 2898f17

Browse files
refactor: improve React Native developer support setup and host resolution
1 parent 5e77cf4 commit 2898f17

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

android/src/main/java/com/mendix/mendixnative/MendixInitializer.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,23 @@ class MendixInitializer(
3333
MxConfiguration.runtimeUrl = runtimeUrl
3434
MxConfiguration.warningsFilter = mendixApp.warningsFilter
3535

36+
// Must run before the first access to `reactHost` below (and before any reload).
37+
// `PackagerConnectionSettings.debugServerHost` caches its value in memory for the
38+
// lifetime of the process the first time it's read (e.g. by an eager dev-support
39+
// settings reload triggered as soon as ReactHost/DevSupportManager is constructed).
40+
// If that first read happens before we persist the real Metro host, RN falls back
41+
// to its hardcoded emulator default (10.0.2.2:8081) and keeps using it for the rest
42+
// of the process — even though the correct host is written to SharedPreferences —
43+
// which is why a fresh app process (restart) "fixes" it but reload within the same
44+
// process does not. Resolving/pushing the host here, before `reactHost` is touched,
45+
// avoids that stale cache entirely.
46+
if (hasRNDeveloperSupport) setupDeveloperApp(runtimeUrl, mendixApp)
47+
3648
// Reload only if there's already a running instance.
3749
if (reactHost.currentReactContext != null) {
3850
reactHost.reload("Clean start for new Mendix app")
3951
}
4052
if (clearData) clearData(context.application)
41-
if (hasRNDeveloperSupport) setupDeveloperApp(runtimeUrl, mendixApp)
4253
}
4354

4455
fun onDestroy() {
@@ -64,6 +75,14 @@ class MendixInitializer(
6475
preferences.setDeltas(false)
6576
preferences.setDevMode((mendixApp.showExtendedDevMenu))
6677

78+
// Explicitly push the freshly resolved host into the live ReactHost's
79+
// PackagerConnectionSettings. Writing to SharedPreferences alone (above) isn't
80+
// enough if RN already cached a stale/default debug server host in memory for
81+
// this process — this overwrite takes effect immediately, regardless of when
82+
// that first (possibly premature) read happened.
83+
reactHost.devSupportManager?.devSettings?.packagerConnectionSettings?.debugServerHost =
84+
preferences.getMetroBundlerHost()
85+
6786
clearCachedReactNativeDevBundle(context.application)
6887
}
6988
}

0 commit comments

Comments
 (0)