Lesson 25. Task. What it is and how it is formed
Android Lessons

Lesson 25. Task. What it is and how it is formed

In this lesson:

– a little theory on Task
– Fix Activity in Paused state

In this tutorial, we learn where Activity is placed until it is visible. And where it comes from when you press the back button. The help is written quite clearly. I will make a brief translation of the main part of this help and use their diagrams.


We already know that an application may contain several Activity. And that Activity can call Activity from other applications using Intent and Intent Filter. If you want to send a letter from your application, you call Activity mail program and transmit data to it. The letter goes and you come back to your program. It seems that all of this was happening within one program. This “seamless” is achieved due to the fact that both Activity (yours and zip) were in the same Task.

Before I continue explaining, I want to make an analogy at once to make it easier to understand. In parentheses, I will give concepts similar to Android.

mechanism of organization Activity in Android very similar in implementation to navigation in the browser. You are in the same tab (Task) And open the pages (Activity) By clicking on the links (Intent). You can return to the previous page at any time by clicking the Back button. But the Forward button is missing because the page on which the Back button was clicked is erased. And we need to click the link again if we want to get to it. If you need to open something new, you create a new tab and now open pages in it, go to the links, go back. As a result, you have several tabs. Most of them are in the background, and one (the active one you are currently working on) is in the front.

As a result, a list of browser analogies and Android such:

browser – Android
Visit History tab – Task
page – Activity
Link – Intent

Now you will have a clearer text about Task.

Task is a group of several Activities that a user performs a certain operation. Usually the starting point for creating a Task is the Home screen.

When in Home, you call any application from the list of applications or through a shortcut. Task is created. And the Application Activity (referred to as MAIN in the manifest file) is placed in this Task as root. Task goes to the front. If, however, when calling the program, the system found that there is already a Task in the background corresponding to this application, then it will bring it to the foreground and create nothing will happen.

When Activity_A calls Activity_B, Activity_B is placed on the top (top) Task and gets the focus. Activity_A stays in Task but is in Stopped state (not visible and out of focus). Further, if the user presses Back while in Activity_B, Activity_B is removed from Task and destroyed. And Activity_A is now at the top of the Task and gaining focus.

In which order the Activity was opened (added to the Task), in that order they are contained in the Task. They are not specifically sorted or sorted inside. The Task Activity Set is also called a back stack. I’ll just call it a stack.

The diagram (from the official site) shows an example:

At the top is what the user sees. At the bottom is Task content. You can see that when you call new Activity, they are added to the top of the stack. And if the Back button is clicked, the top Activity is removed from the stack and the previous Activity is displayed.

Let’s say we have a Task with several Activity. He is in the foreground, we are working with him now.

– if we press the Home button, nothing will be deleted, all Activity will be saved in this Task-e, and Task itself will simply go to the background and it can always be called from there, again calling the application whose Activity is the root of the Task. . Or you can hold down the Home button and we’ll see just a list of Task’s that are in the background.

– if in the Active Task-e repeatedly press the Back button, then in the end there will be no Activity, the empty Task will be removed and the user will see the Home screen.

There is always a lot of nuances and difficulties, but for now we will dwell on this and in the bowels did not climb. This knowledge is sufficient to answer the questions of the previous lesson: why in step 2 MainActivity disappeared from the screen but left in memory and not destroyed? After all, in step 3 it was destroyed ActivityTwo after it has disappeared from the screen. And in step 4, the result was also destroyed MainActivity. Why was Step 2 an exception?

Now you know why. Because in Step 2 MainActivity was left in the stack, and ActivityTwo put it on top of the stack and got the focus. Well, in steps 3 and 4, Activity was removed from the top of the stack, there was no Activity in Task, and we saw the Home screen.

If we in Step 3 did not press Back but Home, then Task with both Activity would leave the background and nothing would be destroyed.


Now let’s open the project from the last lesson of P0241_TwoActivityState. We wanted to catch the Paused status for Activity. This state means that Activity is out of focus, but visible, albeit partially. We can achieve this by providing a dialog style for ActivityTwo. It will appear as a popup and underneath it will be partially visible MainActivity – it will be in Paused status. Let’s implement.

To do this, open AndroidManifest.xml, the Application tab, find ActivityTwo there, and to the right of the field Theme write the following text: @android: style / Theme.Dialog

We all save and launch the application.

MainActivity appeared


MainActivity: onCreate ()
MainActivity: onStart ()
MainActivity: onResume ()

Everything is correct.

Call ActivityTwo.


MainActivity: onPause ()
ActivityTwo: onCreate ()
ActivityTwo: onStart ()
ActivityTwo: onResume ()

We see that it was not typed onStop method for MainActivity, so the application was not translated into the Stopped state and is in Paused mode.

Click Back.


ActivityTwo: onPause ()
MainActivity: onResume ()
ActivityTwo: onStop ()
ActivityTwo: onDestroy ()

MainActivity resumed on the onResume call only, and didn’t need onStart because it was Paused, not Stopped.

We clearly saw the difference between this example and it in the last lesson. And we had MainActivity in the Paused state.

Next you can click Back, or you can Home – you already know what will happen in both cases. By logs you can see this.

To return ActivityTwo to normal display mode, go back to the manifest and remove the line from the Theme field.

By the way, you already have enough knowledge to create an application with a bunch of Activity, make calls and play, look at logs. Do so with the LifeCycle and Task themes.

In the next lesson:

– Call Activity using implicit call and Intent Filter

Discuss in the forum [32 replies]

Leave a Reply

Your email address will not be published. Required fields are marked *