Most of this paper is translated from Android “launchMode” (Visualized), and some supplementary contents are added in SingleInstance based on my own understanding
Startup mode is an important area of knowledge that every Android developer should understand how it works. Most developers (myself included) think we already know how to use boot mode. But we were wrong. It’s not just enough to know to set the Activity to SingleTask to prevent repeated instantiations, although in most cases this solves our business needs. But do we really understand how when we set an Activity to SingleTask, it affects other Activites in the Activity task stack?
This paper will illustrate the role and difference of the mode of starting in 4. First, I need to explain the following pictures to make it easier for you to understand
Green means that a new Activity instance has been created in the Activity stack and yellow means that the Activity instance has been reused, but the Activity’s onNewIntent() method will be called
Standard startup mode description
If you set Activity B’s startup mode to Standard in androidmanifest.xml, when you start Activity B, regardless of whether an instance of Activity B already exists in the task stack, Creates a brand new Activity B at the top of the task stack
SingleTask mode explanation
If the Activity C startup mode is set to SingleTask in androidmanifest.xml, if an instance of Activity C already exists in the task stack and is not at the top of the stack, when the Activity C is started, All instances above Activity C are pushed off the stack to the top of the task stack and the onNewIntent() method in Activity C is called
If the Activity C startup mode is set to SingleTask in androidmanifest.xml and there is no instance of Activity C in the task stack, when the Activity C is started, A brand new Activity C instance is created at the top of the stack
SingleTop startup mode explanation
If Activity D is set to SingleTop in androidmanifest.xml and there is an instance of Activity D at the top of the stack, when Activity D is started, The previously created instance of Activity D is reused, and the onNewIntent() method is called
If Activity D is set to SingleTop in androidmanifest.xml and there is no instance of Activity D in the task stack, when Activity D is started, A brand new Activity D instance is created at the top of the stack
If Activity D is set to SingleTop in androidmanifest.xml and there is an instance of Activity D in the task stack but it is not at the top of the stack, when Activity D is started, A brand new Activity D instance is created again at the top of the stack
SingleInstance mode explanation
If the Activity E startup mode is set to SingleInstance in androidmanifest.xml and there is no instance of Activity E in the task stack, a new task stack will be created when the Activity E is started. And there is only one instance of Activity E in the stack
If an Activity F starts in Standard mode from Activity D based on the task stack above, what happens to the task stack? Please see below
At this point, Activity E will still exist independently in its own task stack, and the newly created Activity F will be at the top of the same task stack as Activity D
If you start Activity E in SingleInstance mode and it already exists in its own task stack, the created instance of Activity E will be reused. And Activity E’s onNewIntent() method is called
If an Activity F starts in Standard mode from Activity E based on the above task stack, how will the task stack change? See the following figure
Because singleInstance’s property forbids sharing the task stack with other Activities, So an Activity in SingleInstance starts other activities with FLAG_ACTIVITY_NEW_TASK by default. So after Activity E starts Activity F, there will be three task stacks, and Activity F will exist in a separate task stack
reference
- Android “launchMode (Visualized)”
- Android Developers