react-native-receive-sharing-intent icon indicating copy to clipboard operation
react-native-receive-sharing-intent copied to clipboard

Crash app when build real device in Android

Open itsminh99 opened this issue 3 years ago • 7 comments

  • 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).

itsminh99 avatar Oct 30 '21 04:10 itsminh99

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

VBarzionov avatar Nov 03 '21 15:11 VBarzionov

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
  }

vlesu avatar Nov 17 '21 14:11 vlesu

Thanks you two. Those changes fixed the issue for me.

alexkendall avatar Nov 26 '21 17:11 alexkendall

@vlesu Shall we also modify clearFileNames() based on the change ? Hope to hear your expertise. --Luke

robbiedood avatar Jan 07 '22 19:01 robbiedood

@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();
...

vlesu avatar Jan 10 '22 15:01 vlesu

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?

OskarD avatar Mar 20 '22 07:03 OskarD

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();
     }
 

jensdev avatar Aug 17 '22 15:08 jensdev