Android’s Activities and Tasks

On Android, one activity can start another activity by using Intent. A task is a stack of activities that relates. The activity starts another activity with Intent, which is pushed to the same stack, and it is popped by the Back button. This is the standard behavior of Android. However, this behavior of the task can be changed by flags given to the Intent and attributes of the activities.

The page “Application Fundamentals” explains this very well. I wanted to know how to use these flags and attributes actually, so I looked for examples.

[1] Sharing from gallery

In the AndroidManifest.xml file of the gallery application, the clearTaskOnLauch attribute is True. So, if you start another activity from the gallery and back to the home, then you can start just a gallery screen by launching the gallery application. Because started activities were cleared when returning to the home.

[2] Starts Gmail from other application

In AndroidManifest.xml of Gmail, the allowTaskReparentng attribute is True. So, the Gmail activity started from other applications that are stacked to a task will move to a new task that has the same affinity as Gmail when the task comes to the foreground. For example,

・starts Gmail, and makes a new message
・return to the home
・starts Gallery, and share some pictures to Gmail
・return to the home

Because the Launch mode of Gmail is neither a single task nor a single instance, at this time there are two tasks 1) Gmail message list – making new messages 2) Gallery – sharing pictures with Gmail.

・ switch to task 1) by starting Gmail.

Then, a screen for sharing pictures is appearing, and by Back button, making new message screen appears. It seems that sharing pictures with Gmail activity of 2) moves to task 1).

[3] Page sharing from a browser

a launch mode of Browser is singleTask. It means the browser is always the route of the task. If some activity starts Browser, It can’t push Browser activity to its own stack. The browser starts a new task. And, the alwaysRetainTaskState attribute is True, which means the state of the task is retained.

However, If you start other applications from the browser, return to the home, and start the browser again, then the browser screen appears. Why?

When the browser starts other applications by “Share page”, the flag named FLAG_ACTIVITY_PREVIOUS_IS_TOP is set to Intent. This flag means that the activity starts with the Intent and seems to finish soon, so regard the previous activity as a top activity of the task. So when you switch the task to the browser, the browser itself is regarded as a top activity of the task. ( I’m not sure if this is true or not)

************************************************************************************************************

This framework of activities and tasks is not for general Android users, but for developers.

I think that developers should design the behavior of activities and tasks in a way that the ordinary user can use without being conscious of the rules. No strange behavior and behave as the same as expected by the user are good designs.

PAGE TOP