gpt4 book ai didi

android - SingleTask Activity ,但通过两个不同的应用程序打开

转载 作者:可可西里 更新时间:2023-11-01 18:47:46 25 4
gpt4 key购买 nike

这张图对理解很有帮助launhmode singleTask 提供的功能,取自 here

enter image description here但是,我在同一应用程序的情况下理解这一点,我在理解时遇到问题 如果两个任务属于两个不同的应用程序怎么办

令人困惑的场景(虚构),

  • 我正在浏览一个应用程序,该应用程序提供了一个要发送的操作电子邮件,我选择了“发送电子邮件”选项。
  • 将选择我手机的默认“电子邮件应用程序”及其 Activity (即声明为单任务)将被打开。
  • 当我输入电子邮件内容时,我切换到某个聊天应用程序并该应用程序崩溃并为我提供了报告问题的选项通过电子邮件发送给开发人员,现在当我选择“报告”时,我的电子邮件应用程序(与默认电子邮件应用程序相同)将打开。

  • 现在电子邮件应用程序的根 Activity 是单任务的,我的内容我写的哪些内容对我可见?

这次最主要的是,任务/堆栈属于两个不同的应用。

最佳答案

即使您使用 2 个不同的应用程序,它也可以在 expected way 中运行:

  • 如果您的singleTask Activity 已经存在,将使用该副本,方法为onNewIntent()。被召唤
  • 如果不存在,则正常启动

从技术上讲,从您的链接中复制定义:

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.


这可以通过 making an activity a target for sharing text 轻松验证和 list 中的 singleTask:

    <activity
android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>

现在向 onCreate()onNewIntent() 方法添加一些日志记录,并进行一些场景测试。


我发现在测试各种启动模式时特别有用的是以下 ADB 命令:

  • adb dumpsys Activity Activity

这会输出大量文本(这可能有助于在执行此操作之前重启手机 - adb reboot)显示 Activity 任务堆栈的详细信息。这可用于向您展示您的 singleTask Activity 在通过不同的应用程序启动时“rehomes”自身。


关于电子邮件的问题,我认为这取决于您使用的电子邮件客户端,但我希望他们正确处理 onNewIntent() 方法,并保存当前草稿在显示您的新电子邮件之前。

关于android - SingleTask Activity ,但通过两个不同的应用程序打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42506307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com