113

属性到底是做什么taskAffinity用的?我已经阅读了文档,但我不太了解。

任何人都可以用外行的术语解释任务亲和力吗?

4

3 回答 3

167

What is Android Task Affinity used for?

An android application has Activities that form a stack like a deck of cards. If you start an android application, and start five activities A,B,C,D,E. They will form a stack

E   - chat view
D   - weather screen
C   - map view
B   - weather screen
A   - login screen

E was the last Activity to be started and it is showing. If you close E, D will be shown. If you close D, C will be shown. etc.

Notice that Activities B and D are the same activity. What if the user were to make some modifications to the D weather screen, and then decided to close the activity, then close the C Map view?

Then the user would be back at the weather screen and the user would be unhappy because the changes made at level D weather screen were not saved in level B weather screen. Although it's the same activity, it's a different STATE of that activity.

The user had a 5 layer stack of activities, and two of them were the same activity. Popping all 5 off the stack will create the phenomenon where you will be interacting with two different versions of the same activity and can be quite confusing.

Users don't usually think in terms of a rigid stack of activities. They think: "ooh the weather view I'll make a change there" and then they want to go back to the Map view. Then back up again because they want to go back to the Login screen. Why is the B weather app showing and why didn't it save the settings from level D?

The programmer might be able to alleviate some confusion if Activities B and D were linked in state. That way changes to one changes the other. Each time the user opens up a new weather screen, it secretly opens the single instance of the weather screen.

In these circumstances, changing the taskAffinity of the Activity might be desirable. The user would change level D. Then back up to level B. And see the changes in B that were made to D.

The program keeps a stack you can backup through, which is nice, and when the user opens up X instances of the same activity in random places, they are all one.

Slideshow with more explanation: http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack

于 2013-07-26T05:22:23.340 回答
3

亲和性指示活动更喜欢属于哪个任务。

亲和力在两种情况下发挥作用:

当启动活动的意图包含 FLAG_ACTIVITY_NEW_TASK 标志时。

当 Activity 的 allowTaskReparenting 属性设置为“true”时。

请参考http://developer.android.com/guide/components/tasks-and-back-stack.html

于 2015-05-06T12:26:20.653 回答
-3

您可以在此详细演示文稿中找到所有案例(有时还有边缘案例)

请参阅操作 Android 任务和返回堆栈

于 2019-06-21T16:10:49.957 回答