The multitasking screen has undergone some huge changes in the 5.0 release. It also has a new name: overview, with the items called documents. Before Lollipop, this screen could only display one item per app, but now it is possible to open multiple tasks per app, each of them appearing as a separate document in the overview. Moreover, the appearance of each document can be tailored to the app - this post will show you how.

Opening multiple Documents and Tasks

Starting an activity with a new document can be specified in the launching intent, by adding the FLAG_ACTIVITY_NEW_DOCUMENT flag. Also, by setting the FLAG_ACTIVITY_MULTIPLE_TASK flag the system creates a new task as well, with the Activity as root. Check out the snippet below:

final Intent intent = new Intent(this, ThirdActivity.class)
    .setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
    .setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);

This can also be done in the manifest, by setting the documentLaunchMode attribute of the activity to either intoExisting (opens a new document, but reuses the current task), or always (opens a new document and starts a new task). The number of documents in the overview can be limited manually in the manifest with the maxRecents attribute. However, this cannot exceed 50, or 25 for devices with low RAM. Also, this number is for the whole application, not for individual activities.

<activity android:maxRecents="8" .../>

Customizing the appearance of Documents

The looks of each card can be specified programmatically, which is a good way of expressing the branding of the app. The title, the icon and the color of the header background can be defining an ActivityManager.TaskDescription; this should be done in the onCreate() function in the Activity.

protected void onCreate(Bundle savedInstanceState) {
    //...
    final ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription("1st Activity", BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon), Color.GREEN); setTaskDescription(taskDescription);
    //...
}

This is available for sites as well - while the document belonging to Chrome has a grey header by default, webpages can override this setting to have a color of their own. The color setting also effects the status bar. A few sites have already adopted this feature, check out for example AndroidPolice in Chrome. Just add the following tag to the head of the page (content specifies the color).

<meta name="theme-color" content="#ababab"/>

The defaults

The document header is customized by default even when nothing is set manually. The icon comes from the android:icon attribute, the title is the android:label attribute, and the color of the header is equal to the colorPrimary value of the theme definition.

Persistence across reboots

The documents in the overview can be reopened across reboots. This can be specified by setting the persistableMode attribute of the activity tag. Three options are available (more info here):

  • persistRootOnly - the default setting. The task persists after a restart, the launch intent is used.
  • persistNever - the task will not persist after a reboot.
  • persistAcrossReboots - the task and the activity persists after a reboot.

Managing the tasks of an application The AppTask class was also introduced in API level 21. This class can be used to list and manage the current tasks of the application.

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.AppTask appTask : activityManager.getAppTasks()) {
    // ...
}

The new multitasking is more than just a gimmick. A proper setup can result in a better user experience with multiple documents, and modifying the appearance of the header can mean a more recognisable brand.