Translated by Taras Leskiv (http://android-by-example.blogspot.com/)
In this lesson we will find out where is Activity placed while it is not visible. And where it is taken from when the back button is pressed. It is explained quite good in the documentation. I will make a brief interpretation of documentation and will use diagrams from it.
We already know that an application can contain several Activities. And that an Activity can invoke Activities of other applications using Intent and Intent Filter. If you want to send mail from your application, you invoke the Activity from mail application and pass it the required data. Message is sent and you come back to your application. So it seems like everything happened in the context of a single application. This workflow is achieved by running two Activities (your and mail) inside one Task.
Before I start explaining, I will try to make an analogy to make it simple for understanding. I will give similar Android concepts in brackets.
The mechanism of Activity organization is very similar to browser navigation implementation. You have currently one tab opened (Task) and you open pages (Activity) by clicking links (Intent). Every moment you can come back to the previous page by clicking Back. But the Forward button is absent as the page on which Back button has been clicked is removed from memory. And if you want to open it, you have to click the link again. If you need to open something new, you create a new tab and then open pages in it, click links and come back when you need to. In the end you have several tabs. Most of them are in the background, but one (currently active, which you are working with) – is in the foreground.
Finally the list of analogies with browser and Android is the following:
Browser – Android
Tab with the history of visited pages – Task
Page – Activity
Link – Intent
Now the text about task will be easier to understand.
Task is a group of several Activities, with the help of which the user performs a specific operation. Usually a starting position for creating a Task is Home screen.
When you are in Home you invoke any application from the list of applications or by clicking its shortcut. And application Activity (which is marked as MAIN in the manifest-file) is put into this Task as the root Activity. Task goes to foreground. If when invoking the application, the system notices that there is already a Task in the background that corresponds to this application, it will bring it to the foreground and will not create anything.
When Activity_A invokes Activity_B, Activity_B is put on the top of the Task and receives focus. Activity_A remains in the Task but is in the Stopped state (is not visible and not focused). After this, when the user clicks the Back button when Activity_B is opened, Activity_B is deleted from the task and destroyed. And Activity_A is now on the top of the Task and gains focus.
Activities are stored in the task in the same order they were opened (and added to the Task). They are not sorted or ordered in any way inside the Task. The set of Activities in the Task is also called back stack. I will just call it – stack.
The diagram (from the official site) demonstrates an example:
In the top part is what the user can see. In the bottom part – contents of the Task. You can see that when you invoke new Activities, they are added on the top of the stack. And when the Back button is pressed, the top Activity is removed from the stack and the previous Activity is displayed
Suppose we have a Task with several Activities. We are currently working with it and it is in the foreground.
– In case we press the Home button, nothing will be removed and all Activities will be saved in the current Task. This Task will go to background and you will be able to invoke this Task by launching the application whose Activity is the root of the Task again. Or you can hold the Home button and we will see the list of Tasks in Background.
– If you press the Back button several times in the active Task, there will be no Activities left in the stack and the empty stack will be removed and the user will see the Home screen.
There are a lot more nuances and complications, but for now, we will stop on this and will not go any further. This knowledge is enough to answer questions from the previous lesson: why in the step 2 MainActivity disappeared from the screen, but was left in memory and was not destroyed? But in the step 3 ActivityTwo was destroyed just after it disappeared from the screen. And in the step 4 in the end MainActivity was destroyed. Why is step 2 exceptional?
Now you know why. Because in the step 2 MainActivity was left in the stack and ActivityTwo was put on the top of the stack and gained focus. And in the steps 3 and 4 Activities were deleted from the top of the stack, there were no Activities left in the stack and we saw the Home screen.
If we had pressed back in the step 3, then Task with both Activities would have gone to the background and nothing would have been destroyed.
Now let’s open the project from previous lesson P0241_TwoActivityState. We wanted to catch Activity in the Paused state. This state means that Activity is not focused, but it is visible, even if partially. We can achieve this if we assign the dialog style for ActivityTwo. It will be displayed as a dialog and MainActivity will be partially visible behind it – MainActivity will be in Paused state. Let’s implement this.
To do so, open AndroidManifest.xml , Application tab, find there ActivityTwo and to the right of it, in the Theme field write the following text: @android:style/Theme.Dialog
Save everything and run the application.
MainActivity has appeared
Everything is correct.
You can see that onStop method hasn’t been invoked for MainActivity and it means that application was not put into the Stopped state and remains in the Paused state.
MainActivity was restored by only onResume invocation, onStart was not needed as it was in the Paused, not Stopped state.
We have clearly noticed the difference between this example and one from the previous lesson. And we had MainActivity in the paused state.
After this you can press Back or Home button – you already know what will happen in both cases. You can check it by reading logs.
To make ActivityTwo to display normal again, go to manifest and delete the line from the theme field.
By the way, you already have enough knowledge to create an application with multiple Activities, make some invocations, play around with it and read logs. You can make your knowledge solid this way.