Hey. From the title of the topic you might have noticed that it will be a question of how Tasks and Back Stack work in Android. This article will be a free translation of the
official source . The topic is more focused on beginners, but I think that an experienced developer will be able to learn something new, because The topic is specific and it is not often necessary to customize the behavior of our Activity.
Introduction
So. Each Android application, at least, consists of the fundamental objects of the system - Activity. An activity is a separate screen that has its own separate logic and UI. The number of Activity in the application is different, from one to many. When navigating between different Activities, the user can always return to the previous, closed Activity when pressing the back button on the device. Similar logic is implemented using the stack stack (Activity Stack). His organization “last in, first out” - i.e. last entered, first out. When you open a new Activity, it becomes the top, and the previous one goes into stop mode. The stack cannot be mixed, it has the ability to add to the top of a new Activity and delete the top current one. The same Activity can be on the stack any number of times.
Task is a set of Activity. Each task contains its own stack. In a standard situation, each application has its own task and its own stack. When the application is minimized, the task goes to the background, but does not die. He keeps his entire stack at the next opening of the application through the manager or through the launcher, the existing task will be restored and continue its work.
Below I show a picture of how the stack works.

If you continue to press the back button, the stack will delete the Activity until the main root remains. If on it the user clicks back, the application closes and the task will die. By the way, I said that when we minimize our application and launch, for example, a new one, then our task just goes into the background and will wait for the moment while we call it. In fact, there is one "but." If we have a lot of tasks in the background or just heavily load your device, it is not likely that the task will die due to lack of system resources. This is not a war of course, but the fact that we lose all our current data and our stack is cleared is for sure. By the way, to avoid data loss in such cases, you should read about
SavingActivityState .
')
Small total
All that we have described above is the standard behavior for an Android application. In most cases, this is how it should be. But if you still want to change something, there is room for it. They will be described further, but for now a small result of what we have already discussed.
- When launching, a new Activity gets to the top of the stack, shifting the current
- When the application is minimized, the task goes to the background and stores its state. After the next launch of the application, the task is restored along with its stack
- When you press back, the current Activity is permanently deleted. The previous one is put on the top of the stack.
- The same Activity can have any number of instances on the stack.
Tasking
There are two ways to change the standard organization of tasks. We can set special attributes in the manifest for each Activity. We can also set special flags for Intent, which launches a new Activity with startActivity (). Note that sometimes the attributes in the manifest and the flags in the Intent can contradict each other. In this case, the flags Intent will take precedence.
LaunchMode attribute
For each Activity in the manifest, you can specify the launchMode attribute. It has several meanings:
- standard - (by default) when you start an Activity, a new instance is created on the stack. An activity can be placed on the stack several times.
- singleTop - Activity may be placed on the stack several times. A new entry in the stack is created only if this Activity is not located at the top of the stack. If it is currently at the top, then the onNewIntent () method will work for it, but it will not be recreated.
- singleTask - creates a new task and sets the Activity root for it, but only in cases when there is no instance of this Activity in any other task. If the Activity is already located in any task, then that instance will open and the onNewIntent () method will be called. It becomes the main one in due time, and all the upper copies are removed, if there are any. Only one instance of such an activity can exist.
- singleInstance is the same as singleTask, but for this Activity a separate task will always be created and it will be the root in it. This flag indicates that the Activity will be the one and only member of its task.
In fact, it does not matter in which task a new Activity is opened. When you press the back button, we still return to the previous task and previous Activity. The only point to consider is the singleTask parameter. If, when opening such an Activity, we get it from another background task, then we completely switch to it and to its stack. The picture below shows this.

Flags
As mentioned above, we can set special flags for Intent, which launches a new Activity. Flags have higher priority than launchMode. There are several flags:
- FLAG_ACTIVITY_NEW_TASK - launches Activity in a new task. If there is already a task with an instance of this Activity, then this task becomes active, and the onNewIntent () method is triggered.
The flag is similar to the singleTop parameter described above. - FLAG_ACTIVITY_SINGLE_TOP - if the Activity starts itself, i.e. it is at the top of the stack, instead of creating a new instance on the stack, the onNewIntent () method is called.
The flag is similar to the singleTop parameter described above. - FLAG_ACTIVITY_CLEAR_TOP - if an instance of this Activity already exists on the stack of this task, then all the Activities that are on top of it are destroyed and this instance becomes the top of the stack. Also called onNewIntent ()
Affinity
By default, all the activities of our application work in the same task. Optionally, we can change this behavior and specify that in one application Activity work in different tasks, or Activity of different applications work in one. To do this, we can specify in the manifest for each Activity the name of the task by the taskAffinity parameter. This is a string value, which should not coincide with the name of the package, because The standard application task is called exactly as our package. In general, this parameter indicates that the Activity will be guaranteed to open in its separate task. This parameter is relevant if we specify the flag FLAG_ACTIVITY_NEW_TASK or set the attribute attribute to allowTaskReparenting = "true". This attribute indicates that the Activity can move between the tasks that launched it and the task that is specified in taskAffinity if one of them becomes active.
Stack cleaning
If the task is in the background for a long time, then the system cleans its stack itself, leaving only the root Activity. This behavior is due to the fact that the user can forget what he did in the application before and most likely entered it again for another purpose. This logic can also be modified using several attributes in the manifest.
- alwaysRetainTaskState - if the flag is set to true for the root Activity, the stack will not be cleaned and fully restored even after a long time.
- clearTaskOnLaunch - if you set the flag to true for the root Activity, the stack will be cleaned instantly as soon as the user leaves the task. The exact opposite of alwaysRetainTaskState
- finishOnTaskLaunch - works in the same way as clearTaskOnLaunch, but can be installed on any Activity and remove it from the stack
This is all for this topic. The article is not improvised, but in fact is a free translation of
official documentation . I recommend collecting an easy example and experimenting with flags and attributes. Some moments, for me personally, were unexpectedly interesting. Any errors and omissions I will consider in HP. Thank.