Support onNewIntent in Bridgeless (#43401)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/43401

Implement `onNewIntent` in Bridgeless by adding it to ReactHostImpl

Changelog:
[Android][Breaking] Implement `onNewIntent` in Bridgeless

Reviewed By: fkgozali, RSNara

Differential Revision: D54703159

fbshipit-source-id: fd8589d8131f4fa57188d493331dc68fb38c4520
This commit is contained in:
Arushi Kesarwani
2024-03-25 10:40:54 +00:00
committed by Nicola Corti
parent 5eea4cda1b
commit dbc83220e0
5 changed files with 52 additions and 7 deletions
@@ -154,6 +154,7 @@ public class com/facebook/react/ReactDelegate {
public fun onHostDestroy ()V
public fun onHostPause ()V
public fun onHostResume ()V
public fun onNewIntent (Landroid/content/Intent;)Z
public fun onWindowFocusChanged (Z)V
public fun shouldShowDevMenuOrReload (ILandroid/view/KeyEvent;)Z
}
@@ -205,6 +206,7 @@ public abstract interface class com/facebook/react/ReactHost {
public abstract fun onHostPause (Landroid/app/Activity;)V
public abstract fun onHostResume (Landroid/app/Activity;)V
public abstract fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
public abstract fun onNewIntent (Landroid/content/Intent;)V
public abstract fun onWindowFocusChange (Z)V
public abstract fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
public abstract fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
@@ -3654,6 +3656,7 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React
public fun onHostPause (Landroid/app/Activity;)V
public fun onHostResume (Landroid/app/Activity;)V
public fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
public fun onNewIntent (Landroid/content/Intent;)V
public fun onWindowFocusChange (Z)V
public fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
public fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
@@ -176,13 +176,7 @@ public class ReactActivityDelegate {
}
public boolean onNewIntent(Intent intent) {
if (!ReactFeatureFlags.enableBridgelessArchitecture) {
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager().onNewIntent(intent);
return true;
}
}
return false;
return mReactDelegate.onNewIntent(intent);
}
public void onWindowFocusChanged(boolean hasFocus) {
@@ -137,6 +137,19 @@ public class ReactDelegate {
return false;
}
public boolean onNewIntent(Intent intent) {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
mReactHost.onNewIntent(intent);
return true;
} else {
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager().onNewIntent(intent);
return true;
}
}
return false;
}
public void onActivityResult(
int requestCode, int resultCode, Intent data, boolean shouldForwardToReactInstance) {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
@@ -123,6 +123,9 @@ public interface ReactHost {
/* To be called when focus has changed for the hosting window. */
public fun onWindowFocusChange(hasFocus: Boolean)
/* This method will give JS the opportunity to receive intents via Linking. */
public fun onNewIntent(intent: Intent)
public fun addBeforeDestroyListener(onBeforeDestroy: () -> Unit)
public fun removeBeforeDestroyListener(onBeforeDestroy: () -> Unit)
@@ -16,6 +16,8 @@ import static java.lang.Boolean.TRUE;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -664,6 +666,36 @@ public class ReactHostImpl implements ReactHost {
"Tried to access onWindowFocusChange while context is not ready"));
}
/* This method will give JS the opportunity to receive intents via Linking.
*
* @param intent The incoming intent
*/
@ThreadConfined(UI)
@Override
public void onNewIntent(Intent intent) {
log("onNewIntent()");
ReactContext currentContext = getCurrentReactContext();
if (currentContext != null) {
String action = intent.getAction();
Uri uri = intent.getData();
if (uri != null
&& (Intent.ACTION_VIEW.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))) {
DeviceEventManagerModule deviceEventManagerModule =
currentContext.getNativeModule(DeviceEventManagerModule.class);
if (deviceEventManagerModule != null) {
deviceEventManagerModule.emitNewIntentReceived(uri);
}
}
currentContext.onNewIntent(getCurrentActivity(), intent);
}
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException("Tried to access onNewIntent while context is not ready"));
}
@Nullable
JavaScriptContextHolder getJavaScriptContextHolder() {
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();