Android overview screen and android overview Screen

Source: Internet
Author: User

Android overview screen and android overview Screen

Article copied over: original address https://developer.android.google.cn/guide/components/recents.html

 

An overview screen (also known as the latest Dynamic screen, recent task list, or recently used application) is a system-level UI that lists recently accessed activities and tasks. You can browse the list and select the task to be restored, or remove it from the list by swiping the clear task. For Android 5.0 (API level 21), multiple instances of the same Activity containing different documents may be displayed as tasks on the overview screen. For example, Google Drive may execute one task for each document in multiple Google Documents. Each document is displayed as a task on the overview screen.

Figure 1.The overview screen of the three Google Drive documents is displayed. Each document is represented by a separate task.

Generally, you should allow the system to define how tasks and activities are displayed on the overview screen without modifying this behavior. However, the application can determine the display mode and time of the Activity on the overview screen. You can useActivityManager.AppTaskClass to manage tasks, useIntentSpecifies the time when an Activity is added to or removed from the overview screen. You can also use<activity>Property to set this behavior in the configuration file.

Add a task to the overview Screen

UseIntentYou can better control the time and method of opening or re-opening a document on the overview screen. Use<activity>Attribute, you can choose to always open the document in the new task, or choose to reuse the existing task for the document.

Use the Intent flag to add a task

You can call this operation to create a new document for an Activity.ActivityManager.AppTaskClassstartActivity()To pass the Intent of the Activity. Insert a logical line break so that the system displays the Activity as a new task on the overview screen.IntentOfaddFlags()Pass in MethodFLAG_ACTIVITY_NEW_DOCUMENTFlag.

Note:FLAG_ACTIVITY_NEW_DOCUMENTFlag replacedFLAG_ACTIVITY_CLEAR_WHEN_TASK_RESETIndicates that the latter has been deprecated since Android 5.0 (API level 21.

If you setFLAG_ACTIVITY_MULTIPLE_TASKThe system will always use the target Activity as the root to create a new task. This setting allows the same document to be opened in multiple tasks. The following code demonstrates how the main Activity performs this operation:

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: UseFLAG_ACTIVITY_NEW_DOCUMENTIndicates that the started Activity must haveandroid:launchMode="standard"Attribute Value (default ).

When the main Activity starts a new Activity, the system searches for the existing task to check whether the Intent of the task matches the Intent component name and Intent data of the Activity. If no task is found or Intent containsFLAG_ACTIVITY_MULTIPLE_TASKThe Activity is used as its root to create a new task. If it is found, the task will be transferred to the foreground and the new Intent will be passedonNewIntent(). The new Activity obtains the Intent and creates a new document on the overview screen, as shown 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);
}
Use Activity property to add a task

In addition, the Activity can also be specified in its list file to always use<activity>Attributeandroid:documentLaunchModeEnter the new task. This attribute has four values, which will produce the following effects when you use this application to open a document:

" intoExisting"
This Activity will reuse existing tasks for the document. This corresponds NoSet FLAG_ACTIVITY_MULTIPLE_TASKFlag, but set FLAG_ACTIVITY_NEW_DOCUMENTThe identifier produces the same effect, as described in the previous section about adding a task using the Intent identifier.
" always"
This Activity creates a new task for the document, even if the document has been opened. Use this value and set it at the same time FLAG_ACTIVITY_NEW_DOCUMENTAnd FLAG_ACTIVITY_MULTIPLE_TASKThe results are the same.
" none”"
This Activity does not create a new task for the document. The overview screen displays a single task for the application. The task starts from any Activity called by the user.
" never"
This Activity does not create a new task for the document. Setting this value will replace FLAG_ACTIVITY_NEW_DOCUMENTAnd FLAG_ACTIVITY_MULTIPLE_TASKBehavior of the flag (if one of the flag is set in Intent), and the overview screen displays a single task for the application, the task will continue to be executed from any Activity called by the user last time.

Note: For DivisionnoneAndneverOther than the value, must be usedlaunchMode="standard"Define Activity. If this attribute is not specified, usedocumentLaunchMode="none".

Remove task

By default, after the Activity ends, the document task is automatically removed from the overview screen. You can useActivityManager.AppTaskClass,IntentFlag or<activity>Property to replace this behavior.

By adding<activity>Attributeandroid:excludeFromRecentsSettrueYou can always exclude tasks from the overview screen.

You can<activity>Attributeandroid:maxRecentsSet it to an integer value and set the maximum number of tasks that an application can include on the overview screen. The default value is 16. When the maximum number of tasks is reached, the least recently used tasks will be removed from the overview screen.android:maxRecentsThe maximum value is 50 (25 for devices with insufficient memory); the value smaller than 1 is invalid.

Remove a task using the AppTask class

In the Activity of creating a new task on the overview screen, you can callfinishAndRemoveTask()The method specifies when to remove the task and end all associated activities.

NewDocumentActivity. java

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

Note: UsefinishAndRemoveTask()MethodFLAG_ACTIVITY_RETAIN_IN_RECENTSMark.

Retain completed tasks

To keep the task on the overview screen (even if its Activity has been completed), you canaddFlags()Pass in MethodFLAG_ACTIVITY_RETAIN_IN_RECENTSFlag.

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<activity>Attributeandroid:autoRemoveFromRecentsSetfalse. The default value of document Activity istrueThe default value of a regular Activity isfalse. Use this attribute to replaceFLAG_ACTIVITY_RETAIN_IN_RECENTSFlag.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.