“我的第一个应用程序”有一个处理“分享” Intent 的 Activity 。它在 AndroidManifest.xml
中的 Activity 如下所示:
<activity
android:name="com.example.foo.myfirstapp.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/jpeg"/>
</intent-filter>
</activity>
在 KitKat 中,将相册中的图像共享到“My First App”会使 MainActivity 成为相册任务的一部分。这是期望的行为。
在 Lollipop 中,将相册中的图像共享到“My First App”会导致启动“My First App”的新实例。如果我查看概览屏幕,相册任务就在那里……然后有一个单独的条目“我的第一个应用程序”。如果我分享另一张图片,我会得到两个“我的第一个应用程序”实例……等等。
问题:如何让 Lollipop 像 KitKat 一样处理分享 Intent ?
这是我所做的:
我注意到从相册发送的 Intent 根据操作系统设置了不同的标志。 (我使用 getIntent()
并查看 mFlags
获得了这些。)
奇巧:0x80001
(524289):FLAG_GRANT_READ_URI_PERMISSION,FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
Lollipop :0x18080001
(403177473):FLAG_GRANT_READ_URI_PERMISSION、FLAG_ACTIVITY_MULTIPLE_TASK、FLAG_ACTIVITY_NEW_DOCUMENT、FLAG_ACTIVITY_NEW_TASK
来自阅读http://developer.android.com/reference/android/content/Intent.html ,似乎是这最后三个标志导致了问题。具体
When paired with FLAG_ACTIVITY_MULTIPLE_TASK both of these behaviors (FLAG_ACTIVITY_NEW_DOCUMENT or FLAG_ACTIVITY_NEW_TASK) are modified to skip the search for a matching task and unconditionally start a new task.
我一直在尝试通过在 AndroidManifest.xml
的 Activity 中指定 android:launchMode
和 android:documentLaunchMode
来“覆盖”这些标志> 没有成功。
来自 http://developer.android.com/reference/android/R.attr.html#documentLaunchMode , 使用 documentLaunchMode “从不” 似乎很有前途,因为
This activity will not be launched into a new document even if the Intent contains Intent.FLAG_ACTIVITY_NEW_DOCUMENT. This gives the activity writer ultimate control over how their activity is used. but this didn't work.
我也考虑过 android:taskAffinity
,但似乎没有办法说“请选择启动你的任何任务”。
最佳答案
恐怕你对此无能为力。它不在你的控制之下。这是“相册”应用启动其“分享”Intent 方式的一个变化。如果它不希望您的 Activity 参与其任务,您不能强制将其加入其中。
如果您对“分享” Activity 的多个实例有疑问,您可以将“分享” Activity 声明为 launchMode="singleTask"
或 launchMode="singleInstance"
(取决于您的需要。但是,这可能会破坏其他内容。
关于android - Lollipop : making my activity stay in the task that launched the share intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27954026/