From ea5882bfb8d679e0972de54b8319dd65ccb92713 Mon Sep 17 00:00:00 2001 From: lazerg Date: Mon, 26 Oct 2026 08:36:26 -0700 Subject: [PATCH] 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 --- .../facebook/react/runtime/ReactHostImpl.kt | 2 +- .../facebook/react/runtime/ReactHostTest.kt | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.kt index 117502ca0ea..efe12982c18 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.kt @@ -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 { /** diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactHostTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactHostTest.kt index 0d391a3725d..e36969e4d30 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactHostTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactHostTest.kt @@ -168,4 +168,38 @@ class ReactHostTest { reactHost.onHostDestroy(activityController.get()) assertThat(reactHost.lifecycleState).isEqualTo(LifecycleState.BEFORE_CREATE) } + + @Test + fun testActivityDestroyedDuringReload() { + val bgTasks = ArrayDeque() + 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()) + } }