5

I have a app that is a share target. It is a single activity architecture with launchMode="singleTask" in the manifest.

All other apps when sharing their data to my app, seem to honor this, so, the same instance gets opened, even if the activity was previously opened.

However Google Files always seem to force a new instance of my Activity and it always stays in their task.

I debugged the intent flags, but I'm not too smart from that:

FLAG_ACTIVITY_FORWARD_RESULT|FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_PREVIOUS_IS_TOP|FLAG_GRANT_READ_URI_PERMISSION|FLAG_RECEIVER_FOREGROUND

How can I prevent this? I do NOT want more than 1 instance of my single Activity. Not even launchMode="singleInstance" works.


<activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:windowSoftInputMode="adjustResize|stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
        </activity>
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
urSus
  • 12,492
  • 12
  • 69
  • 89
  • "I debugged the intent flags" -- to be clear, that is the list of flags on the `Intent` that was used to start your activity? Also, can you post your manifest entry for this activity? – CommonsWare Sep 01 '19 at 23:24
  • yes the flags on the received intent in my activity. Activity manifest entry is nothing special, but I've added it into the post for you – urSus Sep 02 '19 at 00:16
  • I'm finding this hard to believe. Can you please launch your app from Google Files and then do adb shell dumpsys activity activities and add the output of that command to your question? – David Wasser Sep 23 '19 at 12:33
  • sure, but you can also observe it easily if you have the Files app, every app is then "trapped" in their task, unless you escape it via what @dominicoder wrote – urSus Sep 24 '19 at 00:33
  • I have the same problem whyen sharing from Google Chrome – amarkovits Sep 08 '20 at 19:29

1 Answers1

2

How can I prevent this? I do NOT want more than 1 instance of my single Activity.

You could try detecting if the flag is missing or if your activity is not the task root and launching a new activity and killing the one that is part of the Google Files task. Something like this:

@Override
protected void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.main);

    if (!isTaskRoot()) { // Or check getIntent().getFlags() for SINGLE_INSTANCE
        Intent newIntent = new Intent(getIntent());
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(newIntent);
        finish();
    }
}

Hope that helps!

nmw
  • 6,664
  • 3
  • 31
  • 32
dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • thanks, thats plan B, but why does it happen in first place? the caller intent flags seem to take precedence over my manifest launchmode? – urSus Sep 02 '19 at 04:13
  • From your description, that would seem to be the case. That sort of makes sense since an app launching an external activity would want control over how that activity is started. You could try looking through the source code to see if you can confirm that this is intentional. – dominicoder Sep 03 '19 at 03:19
  • Source of what. Files is not opensources – urSus Sep 03 '19 at 03:33
  • Sorry - the Android source code. Somewhere the system takes an Intent from one app and launches another. If you can find where that happens, you can see if the system takes the flags exactly as they are sent or if it modifies them based on the manifest. Or, put together a sample app that tries to replicate what Files is doing. – dominicoder Sep 04 '19 at 04:17
  • not really the case, since if I share from other apps, they do the right thing according to my my manifest, files needs to do something special – urSus Sep 05 '19 at 07:10
  • @dominicoder - This has just been very helpful to me so thank you. Do you think this is a bug in the Google Files app? It's still doing it to this day. – scgough Dec 18 '20 at 11:23
  • @scgough Sorry, but I'm in no position to say if this is a bug. Could be. Could be intentional. I have no way of knowing. I would suggest you try to find out where you could report this issue to the Google Files team and see if you can get a direct answer. – dominicoder Dec 20 '20 at 04:29