Skip to content

Commit dbc8322

Browse files
arushikesarwani94cortinico
authored andcommitted
Support onNewIntent in Bridgeless (#43401)
Summary: Pull Request resolved: #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
1 parent 5eea4cd commit dbc8322

5 files changed

Lines changed: 52 additions & 7 deletions

File tree

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public class com/facebook/react/ReactDelegate {
154154
public fun onHostDestroy ()V
155155
public fun onHostPause ()V
156156
public fun onHostResume ()V
157+
public fun onNewIntent (Landroid/content/Intent;)Z
157158
public fun onWindowFocusChanged (Z)V
158159
public fun shouldShowDevMenuOrReload (ILandroid/view/KeyEvent;)Z
159160
}
@@ -205,6 +206,7 @@ public abstract interface class com/facebook/react/ReactHost {
205206
public abstract fun onHostPause (Landroid/app/Activity;)V
206207
public abstract fun onHostResume (Landroid/app/Activity;)V
207208
public abstract fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
209+
public abstract fun onNewIntent (Landroid/content/Intent;)V
208210
public abstract fun onWindowFocusChange (Z)V
209211
public abstract fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
210212
public abstract fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
@@ -3654,6 +3656,7 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React
36543656
public fun onHostPause (Landroid/app/Activity;)V
36553657
public fun onHostResume (Landroid/app/Activity;)V
36563658
public fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
3659+
public fun onNewIntent (Landroid/content/Intent;)V
36573660
public fun onWindowFocusChange (Z)V
36583661
public fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
36593662
public fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,7 @@ public boolean onBackPressed() {
176176
}
177177

178178
public boolean onNewIntent(Intent intent) {
179-
if (!ReactFeatureFlags.enableBridgelessArchitecture) {
180-
if (getReactNativeHost().hasInstance()) {
181-
getReactNativeHost().getReactInstanceManager().onNewIntent(intent);
182-
return true;
183-
}
184-
}
185-
return false;
179+
return mReactDelegate.onNewIntent(intent);
186180
}
187181

188182
public void onWindowFocusChanged(boolean hasFocus) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ public boolean onBackPressed() {
137137
return false;
138138
}
139139

140+
public boolean onNewIntent(Intent intent) {
141+
if (ReactFeatureFlags.enableBridgelessArchitecture) {
142+
mReactHost.onNewIntent(intent);
143+
return true;
144+
} else {
145+
if (getReactNativeHost().hasInstance()) {
146+
getReactNativeHost().getReactInstanceManager().onNewIntent(intent);
147+
return true;
148+
}
149+
}
150+
return false;
151+
}
152+
140153
public void onActivityResult(
141154
int requestCode, int resultCode, Intent data, boolean shouldForwardToReactInstance) {
142155
if (ReactFeatureFlags.enableBridgelessArchitecture) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactHost.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public interface ReactHost {
123123
/* To be called when focus has changed for the hosting window. */
124124
public fun onWindowFocusChange(hasFocus: Boolean)
125125

126+
/* This method will give JS the opportunity to receive intents via Linking. */
127+
public fun onNewIntent(intent: Intent)
128+
126129
public fun addBeforeDestroyListener(onBeforeDestroy: () -> Unit)
127130

128131
public fun removeBeforeDestroyListener(onBeforeDestroy: () -> Unit)

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import android.app.Activity;
1717
import android.content.Context;
1818
import android.content.Intent;
19+
import android.net.Uri;
20+
import android.nfc.NfcAdapter;
1921
import android.os.Bundle;
2022
import androidx.annotation.NonNull;
2123
import androidx.annotation.Nullable;
@@ -664,6 +666,36 @@ public void onWindowFocusChange(boolean hasFocus) {
664666
"Tried to access onWindowFocusChange while context is not ready"));
665667
}
666668

669+
/* This method will give JS the opportunity to receive intents via Linking.
670+
*
671+
* @param intent The incoming intent
672+
*/
673+
@ThreadConfined(UI)
674+
@Override
675+
public void onNewIntent(Intent intent) {
676+
log("onNewIntent()");
677+
678+
ReactContext currentContext = getCurrentReactContext();
679+
if (currentContext != null) {
680+
String action = intent.getAction();
681+
Uri uri = intent.getData();
682+
683+
if (uri != null
684+
&& (Intent.ACTION_VIEW.equals(action)
685+
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))) {
686+
DeviceEventManagerModule deviceEventManagerModule =
687+
currentContext.getNativeModule(DeviceEventManagerModule.class);
688+
if (deviceEventManagerModule != null) {
689+
deviceEventManagerModule.emitNewIntentReceived(uri);
690+
}
691+
}
692+
currentContext.onNewIntent(getCurrentActivity(), intent);
693+
}
694+
ReactSoftExceptionLogger.logSoftException(
695+
TAG,
696+
new ReactNoCrashSoftException("Tried to access onNewIntent while context is not ready"));
697+
}
698+
667699
@Nullable
668700
JavaScriptContextHolder getJavaScriptContextHolder() {
669701
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();

0 commit comments

Comments
 (0)