diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java index b375d1859dbc..0f1c1a1a3971 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java @@ -25,6 +25,7 @@ public abstract class ReactActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, PermissionAwareActivity { private final ReactActivityDelegate mDelegate; + private boolean mHasResumed = false; // Due to enforced predictive back on targetSdk 36, 'onBackPressed()' is disabled by default. // Using a workaround to trigger it manually. @@ -74,6 +75,7 @@ protected void onPause() { @Override protected void onResume() { super.onResume(); + mHasResumed = true; mDelegate.onResume(); } @@ -132,6 +134,11 @@ public void invokeDefaultOnBackPressed() { @Override public void onNewIntent(Intent intent) { + // Until the activity first resumes, a new intent belongs to its launch, as when a link + // relaunches a reclaimed task: Linking.getInitialURL() reads it from getIntent(). + if (!mHasResumed) { + setIntent(intent); + } if (!mDelegate.onNewIntent(intent)) { super.onNewIntent(intent); } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt new file mode 100644 index 000000000000..1b2bc0a79edb --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt @@ -0,0 +1,95 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react + +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import com.facebook.react.bridge.Promise +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.modules.intent.IntentModule +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.mock +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.android.controller.ActivityController + +@RunWith(RobolectricTestRunner::class) +class ReactActivityTest { + + @Test + fun onNewIntent_beforeTheFirstResume_makesTheDeliveredIntentTheActivityIntent() { + val controller = createActivity(FIRST_LINK) + val link = viewIntent(SECOND_LINK) + + controller.newIntent(link) + + assertThat(controller.get().intent).isSameAs(link) + } + + @Test + fun getInitialURL_answersTheLinkDeliveredBeforeTheFirstResume() { + val controller = createActivity(FIRST_LINK) + controller.newIntent(viewIntent(SECOND_LINK)) + + verify(requestInitialURL(controller.get())).resolve(SECOND_LINK) + } + + @Test + fun getInitialURL_keepsTheLaunchLinkWhenALinkArrivesAfterTheActivityResumed() { + val controller = createActivity(FIRST_LINK).start().resume().pause().stop() + controller.newIntent(viewIntent(SECOND_LINK)) + + verify(requestInitialURL(controller.get())).resolve(FIRST_LINK) + } + + @Test + fun getInitialURL_answersTheLaunchLinkWhenNoOtherArrives() { + val controller = createActivity(FIRST_LINK) + + verify(requestInitialURL(controller.get())).resolve(FIRST_LINK) + } + + private fun createActivity(link: String): ActivityController { + val controller = Robolectric.buildActivity(TestReactActivity::class.java, viewIntent(link)) + controller.get().setTheme(R.style.Theme_ReactNative_AppCompat_Light) + return controller.create() + } + + private fun requestInitialURL(activity: ReactActivity): Promise { + val context = mock() + whenever(context.currentActivity).thenReturn(activity) + val promise = mock() + IntentModule(context).getInitialURL(promise) + return promise + } + + private fun viewIntent(link: String): Intent = Intent(Intent.ACTION_VIEW, Uri.parse(link)) + + class TestReactActivity : ReactActivity() { + override fun createReactActivityDelegate(): ReactActivityDelegate = + object : ReactActivityDelegate(this, "Test") { + override fun onCreate(savedInstanceState: Bundle?) = Unit + + override fun onResume() = Unit + + override fun onPause() = Unit + + override fun onNewIntent(intent: Intent?): Boolean = true + } + } + + private companion object { + const val FIRST_LINK = "rntester://first" + const val SECOND_LINK = "rntester://second" + } +}