The meaning of FLAG_ACTIVITY_SINGLE_TOP in Android

2019-01-28 09:54发布

问题:

In intent I can set such flag as FLAG_ACTIVITY_SINGLE_TOP. Can somebody explain me its meaning, cause I just don't get it? =)

回答1:

The tasks page says for single top:

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the the activity at the top of the back stack is not an existing instance of the activity).

For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default "standard" launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is "singleTop", the existing instance of D is deliverd the intent through onNewIntent(), because it's at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is "singleTop".



回答2:

Javadoc says:

If set, the activity will not be launched if it is already running at the top of 
the history stack.