Why Behaviours are different ?- android:launchMode=“singleTask” , android:taskAffinity=“” And Intent.FLAG_ACTIVITY_NEW_TASK

[亡魂溺海] 提交于 2019-11-30 15:14:52

I've reproduced the behaviour as you've described.

The first scenario is basically undocumented because a singleTask Activity should always be the root of a task. Because Android uses taskAffinity when determining how and where to launch an Activity, your singleTask Activity is being launched into the existing task, but not as the root Activity. This makes the behaviour different from the behaviour that is documented. When you launch B from D, Android needs to deliver the Intent to B in a call to onNewIntent(), which it cannot do with C and D sitting on top of B. So Android finishes C and D and then calls onNewIntent() on B.

The second scenario shows that you now have 2 tasks, because the singleTask Activity is launched into a new task as the root Activity. Again, when D launches B, Android needs to deliver the Intent to B by calling onNewIntent(), which it cannot do if C and D are in the way. So Android finishes C and D and then delivers the Intent to B in onNewIntent().

The third scenario shows the use of FLAG_ACTIVITY_NEW_TASK. When A launches B with FLAG_ACTIVITY_NEW_TASK, Android looks for an existing task that has B as the root Activity. Since it doesn't find one, it launches B in a new task as the root Activity. When D launches B with FLAG_ACTIVITY_NEW_TASK, Android looks for a task that has B as the root Activity, and if it finds one, it brings that task to the foreground but does not deliver the Intent to onNewIntent(). This is just a way to bring an existing task to the foreground in the state that it was in when it went to the background. This is what you are seeing. This behaviour is also documented (sort-of), but due to the very complex interdependencies between all of these scenarios, all possible cases are not explicitly documented. Sometimes you need to discover how this works by empirical observation instead of reading the documentation.

Thanks for the challenge.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!