棒棒糖:让我的活动留在启动分享意图的任务中 [英] Lollipop: making my activity stay in the task that launched the share intent
问题描述
我的第一个应用程序"具有处理共享"意图的活动.它在AndroidManifest.xml
中的活动如下:
"My First App" has an activity that handles a "share" intent. Its activity in AndroidManifest.xml
looks like this:
<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中,将相册中的图像共享到我的第一个应用程序"会使MainActivity成为相册任务的一部分.这是所需的行为.
In KitKat, sharing an image from the album to "My First App" causes MainActivity to be part of the album’s task. This is the desired behavior.
在Lollipop中,将相册中的图像共享到我的第一个应用程序"会导致启动我的第一个应用程序"的新实例.如果我查看概述屏幕,那么相册任务就在其中...然后有一个单独的条目用于我的第一个应用程序".如果我共享另一个图像,那么我会看到两个我的第一个应用程序"实例...等.
In Lollipop, sharing an image from the album to "My First App" causes a new instance of "My First App" to be launched. If I look at the overview screen, the album task is there...and then there's a separate entry for "My First App". If I share another image, I wind up with two instances of "My First App"...etc.
问题:如何使棒棒糖以与KitKat相同的方式处理共享意图?
这是我所做的:
我注意到从专辑发送的意图根据操作系统设置了不同的标志. (我是使用getIntent()
并查看了mFlags
的.)
Here's what I've done:
I notice that the intents sent from the Album have different flags set depending on the OS. (I got these using getIntent()
and looking at mFlags
.)
Kitkat:0x80001
(524289):FLAG_GRANT_READ_URI_PERMISSION,FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
Kitkat: 0x80001
(524289): FLAG_GRANT_READ_URI_PERMISSION, FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
棒棒糖:0x18080001
(403177473):FLAG_GRANT_READ_URI_PERMISSION,FLAG_ACTIVITY_MULTIPLE_TASK,FLAG_ACTIVITY_NEW_DOCUMENT,FLAG_ACTIVITY_NEW_TASK
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 ,看来这最后三个标志正在引起此问题.具体地
From reading http://developer.android.com/reference/android/content/Intent.html, it seems that these last three flags are causing the problem. Specifically
与FLAG_ACTIVITY_MULTIPLE_TASK配对时,这两个行为(FLAG_ACTIVITY_NEW_DOCUMENT或FLAG_ACTIVITY_NEW_TASK)都被修改为跳过对匹配任务的搜索并无条件启动新任务.
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
的活动中指定android:launchMode
和android:documentLaunchMode
来覆盖"这些标志,但没有成功.
I’ve been attempting to "override" these flags by specifying android:launchMode
and android:documentLaunchMode
in the activity in AndroidManifest.xml
without success.
来自 http://developer.android.com/reference/android/R.attr.html#documentLaunchMode ,将documentLaunchMode设置为从不"似乎是有希望的,因为
From http://developer.android.com/reference/android/R.attr.html#documentLaunchMode, using documentLaunchMode "never" seems promising, since
即使Intent包含Intent.FLAG_ACTIVITY_NEW_DOCUMENT,该活动也不会启动到新文档中.这使活动编写者可以最终控制其活动的使用方式. 但这没用.
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
,但是似乎没有办法说请喜欢启动您的任务".
I also considered android:taskAffinity
, but there doesn’t seem to be a way to say "please prefer whatever task launched you".
推荐答案
害怕您对此无能为力.它不在您的控制之下.这是相册"应用程序启动其共享"意图的方式的一种变化.如果它不希望您的活动"完成任务,则不能将其强制执行.
Afraid you can't do anything about this. It isn't under your control. This is a change in the way the "Album" app is launching its "share" Intent. If it doesn't want your Activity in its task, you can't force it in there.
如果在共享"活动的多个实例方面遇到问题,则可以将共享"活动声明为launchMode="singleTask"
或launchMode="singleInstance"
(取决于您的需求.但是,这可能会破坏其他情况.
If you have issues with having multiple instances of your "share" activity, you could declare your "share" activity as launchMode="singleTask"
or launchMode="singleInstance"
(depending on your needs. This may, however, break other things.
这篇关于棒棒糖:让我的活动留在启动分享意图的任务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!