Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash app when build real device in Android #110

Open
itsminh99 opened this issue Oct 30, 2021 · 7 comments
Open

Crash app when build real device in Android #110

itsminh99 opened this issue Oct 30, 2021 · 7 comments

Comments

@itsminh99
Copy link

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

@VBarzionov
Copy link

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

@vlesu
Copy link

vlesu commented Nov 17, 2021

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
  }

@alexkendall
Copy link

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

@robbiedood
Copy link

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

@vlesu
Copy link

vlesu commented Jan 10, 2022

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

mackenziekira added a commit to quicksoinc/react-native-receive-sharing-intent that referenced this issue Mar 14, 2022
mackenziekira added a commit to quicksoinc/react-native-receive-sharing-intent that referenced this issue Mar 16, 2022
Rather than setting module-level `isClear` boolean, which would only allow the app to receive & respond to one share.

This change prevents the issue on Android of the app 1) receives a share intent, 2) user then opens photo picker, causing app to go to bg and back to fg, 3) triggers share sheet to reopen, because original share intent info has not been properly cleared

ajith-ab/react-native-receive-sharing-intent#110 (comment)
@OskarD
Copy link

OskarD commented Mar 20, 2022

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?

mackenziekira added a commit to quicksoinc/react-native-receive-sharing-intent-fork that referenced this issue May 17, 2022
mackenziekira added a commit to quicksoinc/react-native-receive-sharing-intent-fork that referenced this issue May 17, 2022
Rather than setting module-level `isClear` boolean, which would only allow the app to receive & respond to one share.

This change prevents the issue on Android of the app 1) receives a share intent, 2) user then opens photo picker, causing app to go to bg and back to fg, 3) triggers share sheet to reopen, because original share intent info has not been properly cleared

ajith-ab#110 (comment)
@jensdev
Copy link

jensdev commented Aug 17, 2022

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

marconioliveira pushed a commit to marconioliveira/react-native-receive-sharing-intent that referenced this issue Feb 15, 2023
stockhuman added a commit to Mythologi-XR/react-native-receive-sharing-intent that referenced this issue Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants