我试图了解 Android 中对任务关联性的需求。我也搜索了其他 SO 答案。
我对单任务启动模式和任务亲和性的组合特别感兴趣。
对于 singleTask 启动模式(或带有 NEW_TASK 标志的 Intent),系统会采取 3 个操作之一:
- 如果活动存在,它将恢复它
- 如果活动不存在,它将寻找具有匹配亲和力的任务来添加活动
- 如果没有找到匹配的任务,系统将创建一个以该活动为根的新任务
我从这个答案中理解了第一个案例的必要性。它讨论了在多个状态下拥有相同的活动,以及它如何在用户体验中产生不一致的问题。
我比较疑惑的是第二种情况——系统为什么要找一个已有的具有相同亲和力的任务?如果不允许此功能,这会满足什么样的用例并且会中断?为什么有必要?
从这个链接:
... if the intent passed to startActivity() contains the
FLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to
house the new activity. Often, it's a new task. However, it doesn't have to be.
If there's already an existing task with the same affinity as the new activity, the
activity is launched into that task. If not, it begins a new task.
同样的链接也谈到task reparenting
. 这是我无法理解的另一个功能。该链接确实提供了一个天气/旅行应用程序的示例:
... suppose that an activity that reports weather conditions in selected cities
is defined as part of a travel application. It has the same affinity as other
activities in the same application (the default application affinity) and it allows
re-parenting with this attribute. When one of your activities starts the weather
reporter activity, it initially belongs to the same task as your activity.
However, when the travel application's task comes to the foreground, the weather
reporter activity is reassigned to that task and displayed within it.
我对这个功能的问题是相似的。我无法判断此功能是满足一些必要的用户体验要求还是只是任务的一个花哨的附加组件?为什么需要跨任务重新设置活动?
有人可以帮我回答以上两个问题吗?