Android's Activities and Tasks

Last edited on Sun, 2012-07-08 17:13

I read a chapter of "Activities and Tasks" on a "Application Fundamentals" page of the developers site to understand the behavior of the activities and tasks.

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

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

[1] Sharing from gallery

In AndroidManifest.xml file of the gallery application, the clearTaskOnLauch attribute is True. So, if you starts another activity from the gallery and back to the home, then you can start just a gallery screen by launching 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 application that stacked to a task will move to a new task that has the same affinity as Gmail when the task comes to foreground. For example,

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

Because the Launch mode of Gmail is neither singleTask nor singleInstance, at this time there are two tasks 1) Gmail message list - making new message 2) Gallery - sharing picture with Gmail.

・ switch to the task 1) by starting Gmail.

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

[3] Page sharing from a browser

a launch mode of Browser is singleTask. It means the browser is always 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, it means state of the task is retained.

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

When the browser starts other application 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 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 regard as a top activity of the task. ( I'm not sure 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 behavior of activities and tasks in a way which the ordinary user can use it without conscious of the rules. No strange behavior and behave as the same as the expects by user are good design.

Comments