You get a bonus - 1 coin for daily activity. Now you have 1 coin

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

Lecture



In this lesson:

- a little theory on Task
- fix the Activity in the Paused state

In this lesson we will find out where the Activity is located until it is visible. And where does it come from when you press the back button. The help is written about it quite clearly. I will do a brief translation of the main part of this help and use their schemes.

Task

We already know that an application can contain multiple Activities. And that Activity can call Activity from other applications using the Intent and Intent Filter. If you want to send an email from your application, you invoke the Activity of the mail program and transmit data to it. The letter leaves and you return to your application. It seems that all this happened within the framework of a single application. Such "seamlessness" is achieved by the fact that both the Activity (yours and the postal one) were in the same Task.

Before I continue to explain, I want to give an analogy right away so that the topic is easier to understand. In parentheses, I will give concepts-analogues from Android .

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

As a result, the list of browser and Android analogies is as follows:

Browser - Android
Tab with the history of visits - Task
Page - Activity
Link - Intent

Now you will understand the text about Task .

Task - a group of several Activities with which the user performs a specific operation. Usually the starting position for creating a Task is the Home screen.

Being in Home you call any application from the list of applications or through a shortcut. Task is created. And the Activity of the application (which is marked as MAIN in the manifest file) is placed in this Task as the root. Task goes to the front background. If, when calling the application, the system found that a Task already exists in the background corresponding to this application, then it will bring it to the forefront and will not create anything.

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

In what order were opened (added to the Task) Activity, in that order they are contained in the Task. They are not specifically sorted and not ordered inside. The Activity set in the Task is also called back stack. I'll just call him - stack.

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

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

At the top is what the user sees. At the bottom - the contents of the Task. You can see how they are added to the top of the stack when calling a new Activity. And if the Back button is pressed, the top Activity from the stack is deleted and the previous Activity is displayed.

Suppose we have a Task with several Activities. He is in the foreground, we are working with him now.

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

- if you press the Back button several times in the active Task, then as a result the Activity will not remain in the stack, the empty Task will be deleted and the user will see the Home screen.

There, as always, there are a lot of nuances and difficulties, but for the time being we’ll dwell on this and won’t get into the wilds. This knowledge is quite enough to answer the questions of the previous lesson: why at step 2 MainActivity disappeared from the screen, but it remained to hang in memory and was not destroyed? Indeed, at Step 3, ActivityTwo was destroyed after it disappeared from the screen. And in step 4, the MainActivity was eventually destroyed . Why was step 2 an exception?

Now you know why. Because in Step 2, MainActivity remained in the stack, and ActivityTwo was inserted at the top of the stack and received focus. Well, at step 3 and 4, the Activity was removed from the top of the stack, there was no Activity left in the Task, and we saw the Home screen.

If at step 3 we had pressed not Back, but Home, then Task with both Activities would have gone back and nothing would have been destroyed.

Paused

Now let's open the project from the last lesson of P0241_TwoActivityState. We wanted to catch the Paused state for Activity. This state means that the Activity is not in focus, but it is visible, albeit partially. We can achieve this by assigning the dialog style to ActivityTwo. It will appear as a pop-up window and the MainActivity will be partially visible below it - it will be in the Paused status. Let's implement.

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

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

All save and run the application.

Appeared MainActivity

Logs:

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

That's right.

Call ActivityTwo.

Logs:

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

We see that the onStop method for MainActivity was not called, which means the application was not transferred to the Stopped state and is in Paused mode.

Click Back.

Logs:

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

MainActivity was restored only by calling onResume, but onStart was not needed, because it was Paused, not Stopped.

We clearly saw the difference between this example and him in the last lesson. And the MainActivity we had was able to Paused.

Then you can click Back, and you can Home - you already know what will happen in both cases. By logs you can see this.

To return ActivityTwo to the 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, set up calls and play around, see the logs. Thus secure the theme LifeCycle and Task.

In the next lesson:

- we cause Activity using an implicit call and Intent Filter


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Mobile Programming (Android IOs)

Terms: Mobile Programming (Android IOs)