diff --git a/docs/html/guide/components/recents.jd b/docs/html/guide/components/recents.jd new file mode 100644 index 0000000000000..b44c682578e95 --- /dev/null +++ b/docs/html/guide/components/recents.jd @@ -0,0 +1,256 @@ +page.title=Overview Screen +page.tags="recents","overview" + +@jd:body + +
+
+ +

In this document

+
    +
  1. Adding Tasks to the Overview Screen +
      +
    1. Using the Intent flag to add a task
    2. +
    3. Using the Activity attribute to add a task
    4. +
    +
  2. +
  3. Removing Tasks +
      +
    1. Using the AppTask class to remove tasks
    2. +
    3. Retaining finished tasks
    4. +
    +
  4. +
+ +

Key classes

+
    +
  1. {@link android.app.ActivityManager.AppTask}
  2. +
  3. {@link android.content.Intent}
  4. +
+ +

Sample code

+
    +
  1. Document-centric Apps
  2. +
+ +
+
+ +

The overview screen (also referred to as the recents screen, recent task list, or recent apps) +is a system-level UI that lists recently accessed +activities and tasks. The +user can navigate through the list and select a task to resume, or the user can remove a task from +the list by swiping it away. With the Android 5.0 release (API level 21), multiple instances of the +same activity containing different documents may appear as tasks in the overview screen. For example, +Google Drive may have a task for each of several Google documents. Each document appears as a +task in the overview screen.

+ + +

Figure 1. The overview screen showing three Google Drive +documents, each represented as a separate task.

+ +

Normally you should allow the system to define how your tasks and +activities are represented in the overview screen, and you don't need to modify this behavior. +However, your app can determine how and and when activities appear in the overview screen. The +{@link android.app.ActivityManager.AppTask} class lets you manage tasks, and the activity flags of +the {@link android.content.Intent} class let you specify when an activity is added or removed from +the overview screen. Also, the +<activity> attributes let you set the behavior in the manifest.

+ +

Adding Tasks to the Overview Screen

+ +

Using the flags of the {@link android.content.Intent} class to add a task affords greater control +over when and how a document gets opened or reopened in the overview screen. When you use the +<activity> +attributes you can choose between always opening the document in a new task or reusing an +existing task for the document.

+ +

Using the Intent flag to add a task

+ +

When you create a new document for your activity, you call the +{@link android.app.ActivityManager.AppTask#startActivity(android.content.Context, android.content.Intent, android.os.Bundle) startActivity()} +method of the {@link android.app.ActivityManager.AppTask} class, passing to it the intent that +launches the activity. To insert a logical break so that the system treats your activity as a new +task in the overview screen, pass the {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} flag +in the {@link android.content.Intent#addFlags(int) addFlags()} method of the {@link android.content.Intent} +that launches the activity.

+ +

Note: The {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} +flag replaces the {@link android.content.Intent#FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET} flag, +which is deprecated as of Android 5.0 (API level 21).

+ +

If you set the {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} flag when you create +the new document, the system always creates a new task with the target activity as the root. +This setting allows the same document to be opened in more than one task. The following code demonstrates +how the main activity does this:

+ +

+DocumentCentricActivity.java

+
+public void createNewDocument(View view) {
+      final Intent newDocumentIntent = newDocumentIntent();
+      if (useMultipleTasks) {
+          newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
+      }
+      startActivity(newDocumentIntent);
+  }
+
+  private Intent newDocumentIntent() {
+      boolean useMultipleTasks = mCheckbox.isChecked();
+      final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class);
+      newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
+      newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet());
+      return newDocumentIntent;
+  }
+
+  private static int incrementAndGet() {
+      Log.d(TAG, "incrementAndGet(): " + mDocumentCounter);
+      return mDocumentCounter++;
+  }
+}
+
+ +

Note: Activities launched with the {@code FLAG_ACTIVITY_NEW_DOCUMENT} +flag must have the {@code android:launchMode="standard"} attribute value (the default) set in the +manifest.

+ +

When the main activity launches a new activity, the system searches through existing tasks for +one whose intent matches the intent component name and the Intent data for the activity. If the task +is not found, or the intent contained the {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} +flag, a new task will be created with the activity as its root. If it finds one, it brings that task +to the front and passes the new intent to {@link android.app.Activity#onNewIntent onNewIntent()}. +The new activity gets the intent and creates a new document in the overview screen, as in the +following example:

+ +

+NewDocumentActivity.java

+
+@Override
+protected void onCreate(Bundle savedInstanceState) {
+    super.onCreate(savedInstanceState);
+    setContentView(R.layout.activity_new_document);
+    mDocumentCount = getIntent()
+            .getIntExtra(DocumentCentricActivity.KEY_EXTRA_NEW_DOCUMENT_COUNTER, 0);
+    mDocumentCounterTextView = (TextView) findViewById(
+            R.id.hello_new_document_text_view);
+    setDocumentCounterText(R.string.hello_new_document_counter);
+}
+
+@Override
+protected void onNewIntent(Intent intent) {
+    super.onNewIntent(intent);
+    /* If FLAG_ACTIVITY_MULTIPLE_TASK has not been used, this activity
+    is reused to create a new document.
+     */
+    setDocumentCounterText(R.string.reusing_document_counter);
+}
+
+ + +

Using the activity attribute to add a task

+ +

An activity can also specify in its manifest that it always launches into a new task by using +the <activity> +attribute, +{@code android:documentLaunchMode}. This attribute has four values which produce the following +effects when the user opens a document with the application:

+ +
+
"{@code intoExisting}"
+
The activity reuses an existing task for the document. This is the same as setting the + {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} flag without setting the + {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} flag, as described in + Using the Intent flag to add a task, above.
+ +
"{@code always}"
+
The activity creates a new task for the document, even if the document is already opened. Using + this value is the same as setting both the {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} + and {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} flags.
+ +
"{@code none”}"
+
The activity does not create a new task for the document. The overview screen treats the + activity as it would by default: it displays a single task for the app, which + resumes from whatever activity the user last invoked.
+ +
"{@code never}"
+
The activity does not create a new task for the document. Setting this value overrides the + behavior of the {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} + and {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} flags, if either of these are set + in the intent, and the overview screen displays a single task for the app, which resumes from + whatever activity the user last invoked.
+
+ +

Note: For values other than {@code none} and {@code never} the +activity must be defined with {@code launchMode="standard"}. If this attribute is not specified, +{@code documentLaunchMode="none"} is used.

+ +

Removing Tasks

+ +

By default a document task is automatically removed from the overview screen when its activity +finishes. You can override this behavior with the {@link android.app.ActivityManager.AppTask} class, +with an {@link android.content.Intent} flag, or with an +<activity> attribute.

+ +

You can always exclude a task from the overview screen entirely by setting the +<activity> +attribute, +{@code android:excludeFromRecents} to {@code true}.

+ +

You can set the maximum number of tasks that your app can include in the overview screen by setting +the <activity> +attribute {@code android:maxRecents} + to an integer value. The default is 16. When the maximum number of tasks is reached, the least +recently used task is removed from the overview screen. The {@code android:maxRecents} maximum value +is 50 (25 on low memory devices); values less than 1 are not valid.

+ +

Using the AppTask class to remove tasks

+ +

In the activity that creates a new task in the overview screen, you can +specify when to remove the task and finish all activities associated with it by calling +the {@link android.app.ActivityManager.AppTask#finishAndRemoveTask() finishAndRemoveTask()} method.

+ +

+NewDocumentActivity.java

+
+public void onRemoveFromRecents(View view) {
+    // The document is no longer needed; remove its task.
+    finishAndRemoveTask();
+}
+
+ +

Note: Using the +{@link android.app.ActivityManager.AppTask#finishAndRemoveTask() finishAndRemoveTask()} method +overrides the use of the {@link android.content.Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS} tag, +discussed below.

+ +

Retaining finished tasks

+ +

If you want to retain a task in the overview screen, even if its activity has finished, pass +the {@link android.content.Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS} flag in the +{@link android.content.Intent#addFlags(int) addFlags()} method of the Intent that launches the activity.

+ +

+DocumentCentricActivity.java

+
+private Intent newDocumentIntent() {
+    final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class);
+    newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
+      android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
+    newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet());
+    return newDocumentIntent;
+}
+
+ +

To achieve the same effect, set the +<activity> +attribute +{@code android:autoRemoveFromRecents} to {@code false}. The default value is {@code true} +for document activities, and {@code false} for regular activities. Using this attribute overrides +the {@link android.content.Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS} flag, discussed previously.

+ + + + + + + diff --git a/docs/html/guide/components/tasks-and-back-stack.jd b/docs/html/guide/components/tasks-and-back-stack.jd index e054313e8f3e5..aaef10ec94943 100644 --- a/docs/html/guide/components/tasks-and-back-stack.jd +++ b/docs/html/guide/components/tasks-and-back-stack.jd @@ -21,7 +21,8 @@ parent.link=activities.html

Articles

    -
  1. Multitasking the Android Way
  2. +
  3. + Multitasking the Android Way

See also

@@ -31,6 +32,7 @@ Navigation
  • {@code <activity>} manifest element
  • +
  • Overview Screen
  • @@ -39,12 +41,12 @@ element

    An application usually contains multiple activities. Each activity should be designed around a specific kind of action the user can perform and can start other -activities. For example, an email application might have one activity to show a list of new email. -When the user selects an email, a new activity opens to view that email.

    +activities. For example, an email application might have one activity to show a list of new messages. +When the user selects a message, a new activity opens to view that message.

    An activity can even start activities that exist in other applications on the device. For -example, if your application wants to send an email, you can define an intent to perform a "send" -action and include some data, such as an email address and a message. An activity from another +example, if your application wants to send an email message, you can define an intent to perform a +"send" action and include some data, such as an email address and a message. An activity from another application that declares itself to handle this kind of intent then opens. In this case, the intent is to send an email, so an email application's "compose" activity starts (if multiple activities support the same intent, then the system lets the user select which one to use). When the email is @@ -53,8 +55,8 @@ though the activities may be from different applications, Android maintains this experience by keeping both activities in the same task.

    A task is a collection of activities that users interact with -when performing a certain job. The activities are arranged in a stack (the "back stack"), in the -order in which each activity is opened.

    +when performing a certain job. The activities are arranged in a stack (the back stack), in +the order in which each activity is opened.

    android:parentActivityName="string" android:permission="string" android:process="string" + android:relinquishTaskIdentity=["true" | "false"] android:screenOrientation=["unspecified" | "behind" | "landscape" | "portrait" | "reverseLandscape" | "reversePortrait" | @@ -139,6 +144,15 @@ useful, for example, in an application like the web browser where there is a lot of state (such as multiple open tabs) that users would not like to lose.

    +
    {@code android:autoRemoveFromRecents}
    +
    Whether or not tasks launched by activities with this attribute remains in the +overview screen until the last activity in the +task is completed. If {@code true}, the task is +automatically removed from the overview screen. This overrides the caller's use of +{@link android.content.Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS}. It must be a boolean value, either +"{@code true}" or "{@code false}".
    + +
    {@code android:clearTaskOnLaunch}
    Whether or not all activities will be removed from the task, except for the root activity, whenever it is re-launched from the home screen — @@ -177,7 +191,7 @@ as described above.
    Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the -activity remains running and its {@link android.app.Activity#onConfigurationChanged +activity remains running and its {@link android.app.Activity#onConfigurationChanged(android.content.res.Configuration) onConfigurationChanged()} method is called.

    Note: Using this attribute should be @@ -271,20 +285,67 @@ restart your activity, even when running on an Android 3.2 or higher device).

    All of these configuration changes can impact the resource values seen by the -application. Therefore, when {@link android.app.Activity#onConfigurationChanged +application. Therefore, when {@link android.app.Activity#onConfigurationChanged(android.content.res.Configuration) onConfigurationChanged()} is called, it will generally be necessary to again retrieve all resources (including view layouts, drawables, and so on) to correctly handle the change.

    +
    {@code android:documentLaunchMode}
    +
    Specifies how a new instance of an activity should be added to a task each time it is +launched. This attribute permits the user to have multiple documents from the same application +appear in the overview screen. + +

    This attribute has four values which produce the following effects when the user opens a document +with the application:

    + + + + + + + + + + + + + + + + + + +
    ValueDescription
    "{@code intoExisting}"The activity reuses the existing task for the document. Using this value is the same as setting + the {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} flag, without setting the + {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} flag, as described in + Using the Intent flag to add a task + .
    "{@code always}"The activity creates a new task for the document, even if the document is already opened. + This is the same as setting both the {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} + and {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} flags.
    "{@code none}"The activity does not create a new task for the activity. This is the default value, which + creates a new task only when {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} is set. + The overview screen treats the activity as it would by default: it displays a single task for + the app, which resumes from whatever activity the user last invoked.
    "{@code never}"This activity is not launched into a new document even if the Intent contains + {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT}. Setting this overrides the behavior + of the {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} and + {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} flags, if either of these are set in + the activity, and the overview screen displays a single task for the app, which resumes from + whatever activity the user last invoked.
    + +

    Note: For values other than "{@code none}" and "{@code never}" the +activity must be defined with {@code launchMode="standard"}. If this attribute is not specified, +{@code documentLaunchMode="none"} is used.

    +
    +
    {@code android:enabled}
    Whether or not the activity can be instantiated by the system — -"{@code true}" if it can be, and "{@code false}" if not. The default value +{@code "true"} if it can be, and "{@code false}" if not. The default value is "{@code true}".

    -The <application> element has its own -enabled +The <application> + element has its own +enabled attribute that applies to all application components, including activities. The <application> and {@code <activity>} attributes must both be "{@code true}" (as they both @@ -294,10 +355,11 @@ is "{@code false}", it cannot be instantiated.

    {@code android:excludeFromRecents}
    Whether or not the task initiated by this activity should be excluded from the list of recently -used applications ("recent apps"). That is, when this activity is the root activity of a new task, -this attribute determines whether the task should not appear in the list of recent apps. Set "{@code -true}" if the task should be excluded from the list; set "{@code false}" if it should be -included. The default value is "{@code false}". +used applications, the +overview screen. That is, when this activity is the root activity of a new +task, this attribute determines whether the task should not appear in the list of recent apps. Set +"{@code true}" if the task should be excluded from the list; set "{@code false}" if it +should be included. The default value is "{@code false}".

    {@code android:exported}
    @@ -550,6 +612,13 @@ document.

    +
    {@code android:maxRecents}
    +
    The maximum number of tasks rooted at this activity in the +overview screen. When this number of entries is reached, the system removes the least-recently +used instance from the overview screen. Valid values are 1 through 50 (25 on low memory devices); +zero is invalid. This must be an integer value, such as 50. The default value is 16. +
    +
    {@code android:multiprocess}
    Whether an instance of the activity can be launched into the process of the component that started it — "{@code true}" if it can be, and "{@code false}" if not. @@ -685,6 +754,20 @@ resource usage. attribute can set a different default process name for all components.
    +
    {@code android:relinquishTaskIdentity}
    +
    Whether or not the activity relinquishes its task identifiers to an activity above it in the +task stack. A task whose root activity has this attribute set to "{@code true}" replaces the base +Intent with that of the next activity in the task. If the next activity also has this attribute set +to "{@code true}" then it will yield the base Intent to any activity that it launches in the same +task. This continues for each activity until an activity is encountered which has this attribute set +to "{@code false}". The default value is "{@code false}". + +

    This attribute set to "{@code true}" also permits the activity's use of the +{@link android.app.ActivityManager.TaskDescription} to change labels, colors +and icons in the overview screen.

    +
    + +
    {@code android:screenOrientation}
    The orientation of the activity's display on the device. diff --git a/docs/html/images/components/recents.png b/docs/html/images/components/recents.png new file mode 100644 index 0000000000000..9809f04badcbc Binary files /dev/null and b/docs/html/images/components/recents.png differ