I’m implementing receiving data from other apps with the following approach (code simplified):
@AndroidEntryPoint
abstract class SharingActivity(
protected val sharingDeepLink: Uri
): ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val sharingIntent = Intent(intent).apply {
setDataAndType(sharingDeepLink, intent.type)
component = ComponentName(this@SharingActivity, MainActivity::class.java)
}
startActivity(sharingIntent)
finish()
}
}
Actual sharing Activities inherit from SharingActivity
with an applicable deep link to the main app:
@AndroidEntryPoint
class ChatsShareActivity: SharingActivity(
sharingDeepLink = Uri
.parse("<scheme>://${AppScreen.Messages.withArgs(isSharing = true)}")
)
// And so on
<activity
android:name=".presentation.screen.app.share.ChatsShareActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:exported="true"
android:windowSoftInputMode="adjustResize"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter android:label="@string/tab_messages">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="video/*" />
<data android:mimeType="text/*" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter android:label="@string/tab_messages">
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="video/*" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
The idea here is to have multiple sharing targets in Android Sharesheet without having to re-implement navigation graphs for each one. So I just start MainActivity
with a deep link to a particular screen, where Jetpack Navigation handles synthetic back stack.
But the problem I’m facing is MainActivity
starts twice in different tasks, and I can’t figure out why. Here’s some additional info:
- Sharing an image from the Google Gallery without invoking a system Sharesheet, the intent received in
SharingActivity
is:
Action: android.intent.action.SEND,
flags: FLAG_GRANT_READ_URI_PERMISSION
data: null,
extras: content://media/external/images/media/1000015908
And in MainActivity
:
flags: FLAG_GRANT_READ_URI_PERMISSION
Action: android.intent.action.SEND,
data: <scheme>://chats?isSharing=true,
type: image*,
extras: content://media/external/images/media/1000015908
Works as expected.
- Sharing an image from the Google Gallery via the Sharesheet, the intent is:
Action: android.intent.action.SEND,
flags: FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_MULTIPLE_TASK
FLAG_ACTIVITY_NEW_DOCUMENT
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_PREVIOUS_IS_TOP
FLAG_GRANT_READ_URI_PERMISSION
FLAG_RECEIVER_FOREGROUND
FLAG_RECEIVER_INCLUDE_BACKGROUND
FLAG_RECEIVER_NO_ABORT
data: null,
extras: content://media/external/images/media/1000015908
But here MainActivity
starts twice:
Task: 23070
flags: FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_MULTIPLE_TASK
FLAG_ACTIVITY_NEW_DOCUMENT
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_PREVIOUS_IS_TOP
FLAG_GRANT_READ_URI_PERMISSION
FLAG_RECEIVER_FOREGROUND
FLAG_RECEIVER_INCLUDE_BACKGROUND
FLAG_RECEIVER_NO_ABORT
Action: android.intent.action.SEND,
data: <scheme>://chats?isSharing=true,
type: image*,
extras: content://media/external/images/media/1000015908
Task: 23071
flags: FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_MULTIPLE_TASK
FLAG_ACTIVITY_NEW_DOCUMENT
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_PREVIOUS_IS_TOP
FLAG_ACTIVITY_TASK_ON_HOME
FLAG_GRANT_READ_URI_PERMISSION
FLAG_RECEIVER_FOREGROUND
FLAG_RECEIVER_INCLUDE_BACKGROUND
FLAG_RECEIVER_NO_ABORT
Action: android.intent.action.SEND,
data: <scheme>://chats?isSharing=true,
type: image*,
extras: content://media/external/images/media/1000015908
Note the additional flags. Where are they coming from?
My current thoughts is that this behavior has something to do with the flags that system Sharesheet adds to its intents in combination with my MainActivity
intents containing data
despite being sharing intents.
Why is this happening and what can I do to fix it?