react-native-receive-sharing-intent
react-native-receive-sharing-intent copied to clipboard
Crash app when build real device in Android
- Version: 2.0.0
I run into emulator, then even though it throws the error Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference.
but sharing is still working. However when building a real device, after pressing share it will redirect to the host app but it maybe crash(restart app).
Look at file ReceiveSharingIntentModule.java
@ReactMethod
public void getFileNames(Promise promise){
Activity mActivity = getCurrentActivity();
if(mActivity == null) { return; }
Intent intent = mActivity.getIntent();
receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
mActivity.setIntent(null); // <-------- this is Not Good!
}
this cause Xcpt in RN Linking.getInitialURL here
public void getInitialURL(Promise promise) {
try {
Activity currentActivity = getCurrentActivity();
String initialURL = null;
if (currentActivity != null) {
Intent intent = currentActivity.getIntent();
String action = intent.getAction(); // <--- access to NULL intent
This also cause problems in another modules (for example, exception in RNPushNotifications...)
In my case, solution in file ReceiveSharingIntentModule.java was:
private final ReactApplicationContext reactContext;
private ReceiveSharingIntentHelper receiveSharingIntentHelper;
private Intent oldIntent; // <-- add this line
...
protected void onNewIntent(Intent intent) {
Activity mActivity = getCurrentActivity();
if(mActivity == null) { return; }
oldIntent = mActivity.getIntent(); // <-- add this line
mActivity.setIntent(intent);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@ReactMethod
public void getFileNames(Promise promise){
Activity mActivity = getCurrentActivity();
if(mActivity == null) { return; }
Intent intent = mActivity.getIntent();
receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
if (oldIntent != null) { // <-- add this line
mActivity.setIntent(oldIntent); // <-- change this line from mActivity.setIntent(null);
} // <-- add this line
}
Thanks you two. Those changes fixed the issue for me.
@vlesu Shall we also modify clearFileNames() based on the change ? Hope to hear your expertise. --Luke
@vlesu Shall we also modify clearFileNames() based on the change ? Hope to hear your expertise. --Luke
Unfortunately, I'm just an enthusiast, not an expert.
I did not modify a code in clearFileNames:
@ReactMethod
public void clearFileNames(){
Activity mActivity = getCurrentActivity();
if(mActivity == null) { return; }
Intent intent = mActivity.getIntent();
receiveSharingIntentHelper.clearFileNames(intent);
}
But I had troubles with imcoming files appears TWICE when I switch to another app and switch back.
To resolve this, I have added into ReceiveSharingIntent.ts
clearFileNames(){
ReceiveSharingIntent.clearFileNames();
}
And use this in my code like this:
ReceiveSharingIntent.getReceivedFiles(async (files) => {
...
for(let i = 0; i < files.length; i++) {
if (files[i].filePath) {
files[i].base64 = await RNFS.readFile(files[i].filePath, "base64");
}
}
....
ReceiveSharingIntent.clearFileNames();
...
I still have this problem with both emulated and real device environments
Edit: Just noticed that it's because the fix hasn't been released yet. When are you planning on releasing it?
Until this merges. You can use the snippet below. Put this in patches/react-native-receive-sharing-intent+2.0.0.patch
and use https://www.npmjs.com/package/patch-package
diff --git a/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java b/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
index f752144..d2542f9 100644
--- a/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
+++ b/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
@@ -18,6 +18,7 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
private ReceiveSharingIntentHelper receiveSharingIntentHelper;
+ private Intent oldIntent;
public ReceiveSharingIntentModule(ReactApplicationContext reactContext) {
super(reactContext);
@@ -30,6 +31,7 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
protected void onNewIntent(Intent intent) {
Activity mActivity = getCurrentActivity();
if(mActivity == null) { return; }
+ oldIntent = mActivity.getIntent();
mActivity.setIntent(intent);
}
@@ -40,7 +42,9 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
if(mActivity == null) { return; }
Intent intent = mActivity.getIntent();
receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
- mActivity.setIntent(null);
+ if (oldIntent != null) {
+ mActivity.setIntent(oldIntent);
+ }
}
@ReactMethod
diff --git a/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts b/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
index 735c191..91dab4b 100644
--- a/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
+++ b/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
@@ -33,7 +33,7 @@ class ReceiveSharingIntentModule implements IReceiveSharingIntent {
}
clearReceivedFiles(){
- this.isClear = true;
+ ReceiveSharingIntent.clearFileNames();
}