LaunchMode, in plain English, defines how an Activity should be launched. So what are the differences between these modes and the application scenarios? Why did Google design these modes? What’s behind these modes?
Task and return stack
Before introducing launchMode, let’s talk about tasks and the Back Stack.
A task is a collection of activities that users interact with when performing a certain job. The activities are arranged In a stack — the back stack — in the order in which each activity is opened.
A task is a series of activities that interact with a user when a specific task is completed. These activities are placed on a stack, the return stack, in the order they were opened.
By definition, activities are stored in the order they were opened. It is not hard to guess that this storage method is to facilitate the rollback operation, and it is not difficult to explain the use of stack storage.
When the user clicks launch app, the app’s return stack comes to the foreground, and if it doesn’t already exist, it creates one. When the current Activity starts another Activity, the new Activity is pushed to the top of the stack. If the user clicks the back button, the current Activity is removed from the stack and destroyed, the previous Activity is resumed, and if the stack is empty, it is destroyed. Activities in the stack are never reordered.
According to whether the return stack is in the foreground, it can be divided into the return stack displayed in the foreground, and the return stack placed in the background. Among them, all activities in the back stack in the background are in the stop state, and the user can manually switch the front and back stack state.
When the system runs out of memory, the system destroys activities in the background first. So here’s the question. What is the priority of the background destruction Activity? Do you destroy an Activity after it has been returned to the stack, or do you simply recycle it as an Activity?
Task management
Tasks and return stacks can be managed through a number of parameter Settings, including launchMode, which we describe in this article.
taskAffinity
TaskAffinity is TaskAffinity, which identifies the name of the return stack required by an Activity. The default is the package name. Activities with the same taskAffinity attribute are placed on the same stack. The affinity of a return stack is determined by the affinity of the Activity at the root of the stack.
The taskAffinity attribute is used primarily in conjunction with singleTask or allowTaskReparenting, and in other cases has no effect. Why is that?
allowTaskReparenting
Its main function is to migrate activities from one stack to another, which is related to Activity taskAffinity.
clearTaskOnLaunch
This property is used to clear all activities in the fallback stack except the root Activity, only for the root Activity. When set to True, only the root Activity is seen each time you re-enter the app.
finishOnTaskLaunch
This property, in contrast to clearTaskOnLaunch, removes the Activity without affecting other activities.
alwaysRetainTaskState
This property holds the state of the return stack, only for the root Activity. Normally, clearing a return stack removes all activities above the root Activity. After this parameter is set, the system saves the current status.
Boot mode
What is the main function of boot mode? As described above for tasks and the return stack, it defines how a new instance of an Activity is associated with the current task. It is itself a way of managing tasks.
Launch mode can be defined in two ways: manifest and Intent flag. One is configuration-like, one is code-level. You can probably guess that there is a higher priority in the hierarchy, but the weakness of the code is that it cannot be set without starting the Activity. Android generally provides dynamic and static mode, routines are roughly the same, some differences between the pros and cons.
The manifest setting and intent Flag both contain modes that the other does not. That’s one of the differences.
launchMode
The launchMode here refers specifically to the Activity’s launchMode attribute. There are four ways of doing this, and I’m not going to go into all the details of these four ways, which I think are pretty clear to you.
standard
The most common mode, the default mode for an Activity, is recreated each time an Activity is started, and can be affiliated to a different task or created multiple times within a task.
It is used in a wide range of scenarios, and is generally used for more than specific needs.
singleTop
If the task is at the top of the stack, the system calls the Activity’s onNewIntent() method instead of creating a new instance. When the user hits the Back button, the current Activity is removed from the stack, rather than falling back to the state before onNewIntent().
There are also a few application scenarios. For example, the search page, each time open, search for some results, click on the details page, and then continue to search. For example, a page opened by notification is updated if it exists, and created if it does not.
singleTask
This mode allows only one instance of the Activity to exist in the system. If the current instance does not exist, it is created. If it already exists, all activities on it are removed from the stack and go to onNewIntent().
SingleTask is suitable as an entry point for the application, and does not create a home page repeatedly when the app is called by other means. For example, MainActivity in general, when other apps call.
singleInstance
This pattern is very similar to singleTask, except that the task holding the Activity can contain only one Activity itself.
SingleInstance is suitable for pages that need to be separated from the application, such as the alarm clock ring screen, from the alarm clock Settings. Another example is the dialing interface of the system.
Intent flags
The setup is discussed here in code, and there are three common ways.
FLAG_ACTIVITY_NEW_TASK
Use a new return stack to start the Activity, similar to the singleTask discussed above
FLAG_ACTIVITY_SINGLE_TOP
Similar to singleTop discussed above
FLAG_ACTIVITY_CLEAR_TOP
This approach does not exist in the launchMode discussed above, and differs from singleTop in that it pushes all activities on top of it off the stack when the instance already exists.
It is often used in conjunction with FLAG_ACTIVITY_NEW_TASK to act as a singleTask.
Back to the question
What are the differences between these modes and the application scenarios?
See launchMode above for answers
Why did Google design these modes?
In terms of usage scenarios, in general, when we open a page, we don’t care if it’s unique. This is the most common requirement, hence the Standard mode, which is also the default mode. When we use a search page, when the top layer is a search page, I don’t want to open another search page, so we have the singleTop mode. When we call our App from other apps, I only want to show one main page, so we have singleTask. In the case of the singleInstance pattern, you want to separate it from the current page.
However, I don’t think Google can list all the scenarios. For example, I want to open a page and record the current path, such as A -> B -> C. In this scenario, none of the four modes are included.
Several startup modes, if deduced from the front, are task and return stack management. According to the state in the stack, it can be roughly divided into the following categories:
- The most common off-stack on-stack (Standard)
- Unique (singleTask) in the current stack
- Global singleInstance
- Stack Top (singleTop)
Is that clear? Are there other forms? There must be, like unique at the bottom of the stack, unique in the stack. But this approach can be equivalent to being unique in the current stack.
Can we deduce that Google is classifying these startup modes based on uniqueness? Intent Flags act as auxiliary actions, such as partial offloading and so on. Of course, these are just my guesses, not necessarily accurate, haha.
What’s behind these modes?
See task and return stack
Is memory reclaimed by Activity or task as a benchmark?
As we know, if the return stack is placed in the background, all activities except the root Activity will be destroyed if the alwaysRetainTaskState property is not set when memory is low. You can be sure to base the collection on the return stack.
Why does the taskAffinity attribute take effect when used with singleTask?
You can classify an Activity’s launchMode into two categories based on whether it is unique on the stack
- Standard, singleTop
- SingleTask and singleInstance
The first type is definitely incompatible with taskAffinity because of its uniqueness. The stack created by singleInstance can only contain itself. By default, a stack is created separately, so setting it is meaningless. SingleTask is unique in the current stack and is suitable for creating a new stack, which is why taskAffinity only works with the root Activity.
The last
The content I wrote may not be correct, and some questions are explained based on the information I have seen. For example, why there are four startup modes of the Activity? If you have accurate answers, PLEASE let me know. In addition, I also hope to correct the mistakes in the article.
Finally, thank you for your browsing, I hope to help you.
Favorite Activity lifecycle
Personal blog