📜 ⬆️ ⬇️

Tasks and Back Stack on Android

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.

image

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.

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:

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.
image

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:


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.


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.

Source: https://habr.com/ru/post/186434/


All Articles