My RN app uses react-native-share-menu to accept files shared from other android apps. This works fine when sharing from the photo gallery for instance, but for the generic 'Files' app on some devices (not all devices or on emulators) this sharing does not work well. The intent is sent and my app is launched or comes to the foreground as it is supposed to, but the app state seems to be 'reset' somehow - resulting in my app performing strangely.
So My question is primarily an Android question, probably to do with Intents and receiving Intents -> what could be different about the Files app on some devices as compared to say, the photo browser?
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize"
android:exported="true"
android:alwaysRetainTaskState="true"
android:documentLaunchMode="never">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
<!-- react-native-share-menu -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*"/>
</intent-filter>
THIS IS THE LOGS FROM THE 'working' intent
03-25 11:16:39.502 672 4613 I ActivityManager: START u0 {act=android.intent.action.SEND typ=application/pdf flg=0xb080001 cmp=com.geo.geoop.dev/com.geo.MainActivity clip={application/pdf U:content://com.google.android.apps.nbu.files.provider/2/3875} (has extras)} from uid 10117, pid -1 03-25 11:16:39.520 672 4613 I ActivityManager: Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is "singleInstance" or "singleTask" 03-25 11:16:39.547 672 4613 I ActivityManager: ->startActivity for ActivityRecord{60fe743 u0 com.geo.geoop.dev/com.geo.MainActivity t44} result:START_TASK_TO_FRONT
This is the logs from the intent that resets my activity state 03-25 11:24:49.029 672 1654 I ActivityManager: START u0 {act=android.intent.action.SEND typ=application/pdf flg=0x13000001 pkg=com.geo.geoop.dev cmp=com.geo.geoop.dev/com.geo.MainActivity clip={application/pdf U:content://com.google.android.apps.nbu.files.provider/2/3876} (has extras)} from uid 10309, pid 11323 03-25 11:24:49.057 672 1654 I ActivityManager: ->startActivity for ActivityRecord{88d66b2 u0 com.geo.geoop.dev/com.geo.MainActivity t47} result:START_TASK_TO_FRONT
The only difference I can see is the flags. There are a few different ones between the two:
the working version has these
0x00080000 FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 0x08000000 FLAG_RECEIVER_NO_ABORT 0x04000000 FLAG_ACTIVITY_CLEAR_TOP
the not working version has these flags
0x10000000 FLAG_ACTIVITY_NEW_TASK 0x02000000 FLAG_ACTIVITY_FORWARD_RESULT
Im now delving into the code of the sharing library my RN app is using to receive the share Intent. The behaviour I was seeing is that if the app was launched from a share intent from Google Files, then it would crash/hang during startup. If it was brought to the foreground by Google Files, then it would appear in its resumed state, then 'reset' and become unusable. After looking at the library code recieving the intent, I found the code below - This is what 'resets' my app state, causing the issues. I removed it, and the app no longer crash/hangs after launch.
However, that code is there for a reason. The behaviour without it, results in the Files app becoming associated with my app, such that its launch icon of my app appears as the icon of the files app, and if I click the files icon , it brings my app to the front.
public void getSharedText(Callback successCallback) {
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
return;
}
// If this isn't the root activity then make sure it is
if (!currentActivity.isTaskRoot()) {
Intent newIntent = new Intent(currentActivity.getIntent());
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
currentActivity.startActivity(newIntent);
ReadableMap shared = extractShared(newIntent);
successCallback.invoke(shared);
clearSharedText();
currentActivity.finish();
return;
}
Intent intent = currentActivity.getIntent();
ReadableMap shared = extractShared(intent);
successCallback.invoke(shared);
clearSharedText();
}
thanks for any help with this, Im not an android expert by any means, and this has me scratching my head