2

根据 Android Lollipop 的变化,参考:

StackOverflow 问题

奶酪工厂博客

我希望当我从我的应用程序启动其他应用程序的活动时,即使行为是默认的(启动模式是标准的),它也应该在新任务中打开。因此,我制作了 2 个测试应用程序来验证相同的行为。但令人惊讶的是,如果没有指定启动模式,另一个应用程序总是在我的应用程序任务中打开。我已经在小米红米 Note 3 (5.1.1)、棉花糖模拟器 (x86) 上对此进行了测试,两者的行为是相同的。我很感激这方面的一些帮助,以及来自 Android 开发者网站的参考链接。

一些代码:

Launching app : 
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
startActivity(intent);
break;

App to be launched : 
<activity android:name="com.android.sample.launchdemo.ActivityB">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
</activity>

在启动应用程序单击按钮时,会触发 Intent 并且活动 B 成功打开,但在同一任务中。提前感谢您的任何帮助/建议。

4

2 回答 2

2

看完文档后,我的感觉是标准模式的工作方式与 Android 5.0 (Lollipop) 之前的工作方式相同。奶酪工厂的博客文章是唯一指定该操作的文章,甚至根据我自己的经验,标准启动模式已经在发送它的同一任务中打开了一个活动(除非通过了意图标志)。如果我错了,有人纠正我,但 Android 文档中没有指定标准模式会打开一个新任务。来自 Android 文档:

“标准”(默认模式):系统在启动它的任务中创建一个新的活动实例,并将意图路由到它。Activity可以被实例化多次,每个实例可以属于不同的任务,一个任务可以有多个实例。查看完整文档

对于您要查找的内容,在启动 时Intent,保证新任务的唯一方法是使用 flag FLAG_ACTIVITY_NEW_TASK。您可以通过调用intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK)或来设置它intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)(后者用于将标志链接在一起,因为该方法将返回意图)。

在进行更多研究后,Android 5.0 Lollipop 中似乎做出的唯一变化(与此相关)是最近的应用程序屏幕可以显示活动的多个实例。

In previous releases, the recents screen could only display only one task for each app that the user interacted with most recently. Now your app can open more tasks as needed for additional concurrent activities for documents. This feature facilitates multitasking by letting users quickly switch between individual activities and documents from the recents screen, with a consistent switching experience across all apps. Examples of such concurrent tasks might include open tabs in a web browser app, documents in a productivity app, concurrent matches in a game, or chats in a messaging app.

To me this seems like the only change relating and the post (cheesefactory and SO) have documentLaunchMode set to create new tasks for each activity (which very well could be the case considering the cheesefactory had a "Gallery" app). Documentation on concurrent documents and documentLaunchMode. documentLaunchMode and the flag FLAG_ACTIVITY_NEW_TASK can be configured to do similar things documentLaunchMode is preferred.

于 2016-06-22T02:36:44.617 回答
0

I've found the documentation below.

https://developer.android.com/guide/components/activities/recents.html

=> when the user is using their browser, and they tap Share > Gmail. The Gmail app's Compose screen appears. Tapping the Recents button at that time reveals Chrome and Gmail running as separate tasks. In lower versions of Android, all of the activities appear as a single task, making the Back button the only means of navigation. Figure 2 shows how the Recents screen looks in Android 5.0 and higher, versus lower versions of the platform. The image on the left screen for Android 5.0 and higher, and the image on the right shows how it appears in lower versions of Android.

and refer to the following link. Lollipop: making my activity stay in the task that launched the share intent

=> By default, launchMode of myfirstapp.MainActivity is "standard" and any intent flags weren't set. But after myfirstapp.MainActivity is started through share action of Album, the intent flag contains FLAG_ACTIVITY_MULTIPLE_TASK, FLAG_ACTIVITY_NEW_DOCUMENT.

When an activity is started through share, an activity which contains share in its Toolbar just sets Intent to its ShareActionProvider, and then ShareActionProvider starts an activity with this Intent - in this case, myfirstapp.MainActivity.

So I think that from Lollipop, the system begins a new task for the activity from a different app whose launchMode is "standard" only when the activity is started through share action.

于 2017-05-04T08:25:39.873 回答