Don't resume the new context without an Activity after a reload on Android (#59474)

Summary:
If the Activity is destroyed while a reload is still running, `ReactHostImpl` finishes the reload by calling `moveToOnHostResume(reactContext, currentActivity)` with `currentActivity` already null. The new context gets `onHostResume(null)` and the lifecycle state becomes `RESUMED`, so when the app is reopened `onHostResume(activity)` returns early and `getCurrentActivity()` stays null until the next pause/resume. That's what makes `PermissionsAndroid.request()` reject with `E_INVALID_ACTIVITY` (and likely the `Alert` error in https://github.com/react/react-native/issues/46201).

The reload branch now only resumes the context when there is an Activity. Otherwise it falls through to the normal path, and the next `onHostResume(activity)` resumes the context with the right Activity.

Fixes https://github.com/react/react-native/issues/59473

## Changelog:

[ANDROID] [FIXED] - Fix `getCurrentActivity()` staying null after the Activity is destroyed during a reload

Pull Request resolved: https://github.com/react/react-native/pull/59474

Test Plan: Added `ReactHostTest.testActivityDestroyedDuringReload`. It queues background work so the Activity can be paused and destroyed before the reload finishes, then resumes a new Activity and checks the new context gets it. On `main` it fails (state is `RESUMED` and the context only got `onHostResume(null)`), and it passes with this change. `./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest` passes and ktfmt is clean. I didn't run the reproducer on a device.

Reviewed By: christophpurrer

Differential Revision: D127564742

Pulled By: javache

fbshipit-source-id: 37a80ed86e799473bfc6ed251f60c5ae58506772
This commit is contained in:
lazerg authored and meta-codesync[bot] committed 2026-10-26 08:36:26 -07:00
1 parent 9c8a368067
commit ea5882bfb8
2 files changed
+35 -1

No files matched your search

@@ -1109,7 +1109,7 @@ public class ReactHostImpl(
* Native is reloading, it seems reasonable to assume that: (1) We must have navigated to a
* React Native screen in the past, or (2) We must be on a React Native screen.
*/
if (isReloading && !isManagerResumed) {
if (isReloading && !isManagerResumed && currentActivity != null) {
reactLifecycleStateManager.moveToOnHostResume(reactContext, currentActivity)
} else {
/**
@@ -168,4 +168,38 @@ class ReactHostTest {
reactHost.onHostDestroy(activityController.get())
assertThat(reactHost.lifecycleState).isEqualTo(LifecycleState.BEFORE_CREATE)
}
@Test
fun testActivityDestroyedDuringReload() {
val bgTasks = ArrayDeque<Runnable>()
reactHost =
spy(
ReactHostImpl(
activityController.get().application,
reactHostDelegate,
mock(),
{ bgTasks.add(it) },
Task.IMMEDIATE_EXECUTOR,
false /* allowPackagerServerAccess */,
false /* useDevSupport */,
),
)
doReturn(null).whenever(reactHost).getOrCreateReactHostInspectorTarget()
doNothing().whenever(reactHost).unregisterInstanceFromInspector(any())
reactHost.start()
while (bgTasks.isNotEmpty()) bgTasks.removeFirst().run()
reactHost.onHostResume(activityController.get())
reactHost.reload("Reload from testing infra")
reactHost.onHostPause(activityController.get())
reactHost.onHostDestroy(activityController.get())
while (bgTasks.isNotEmpty()) bgTasks.removeFirst().run()
assertThat(reactHost.lifecycleState).isEqualTo(LifecycleState.BEFORE_CREATE)
val newActivity = Robolectric.buildActivity(Activity::class.java).create().start().resume()
reactHost.onHostResume(newActivity.get())
val newReactContext = mockedBridgelessReactContextCtor.constructed().last()
verify(newReactContext).onHostResume(newActivity.get())
}
}