Analysis of the relationship between Android multi-task switching and activity startup mode Singletask

Source: Internet
Author: User

This will be analyzed in a number of scenarios, and the basic concepts should be understood before the analysis.

Task tasks: A collection of activity that is arranged in the form of a stack (LIFO).

When will the system create a new task task?

This is to be differentiated by app (note, here to see if the activity belongs to the same registration), when an app launches another app's activity in Singletask startup mode, a new task task is created, The activity of the second app becomes the root of the stack.

Conversely, when does a new task not be created? A new task is not created when an app launches another app in a non-singletask manner, or when another activity is started in the same app, regardless of how it was launched.

The above statement will be validated in a later scenario.

When you press and hold the home key, you can see the newly created task in the recently opened tasks.

Each application is started by launch, first to analyze the activity of launch. Launch is initiated by the system (initiated by Activitymanangerservice), and when launch is started, it is equivalent to starting a task, or task1,launch the activity becomes the root element in the Task1-maintained stack. Here, when clicking the App1 boot icon in launch does not continue to add in Task1, it will create a new task task for App1 for the following reasons:

Java code
    1. boolean startactivitysafely (Intent Intent, Object tag) {
    2. Intent.addflags (Intent.flag_activity_new_task);
    3. try {
    4. StartActivity (Intent);
    5. return true;
    6. } catch (Activitynotfoundexception e) {}
    7. }

Launch start other activity is initiated in new task mode. This creates a new task (if the task does not exist) for each app that launches. So many tasks are generated, click the Home button will start launch, equivalent to the launch this task from the background to the foreground, this time continue to click App1, it is equivalent to App1 (premise App1 's first activity in the configuration file of the start Move to the foreground display without re-arranging the location of all the activity in the App1. Aingletask.

Simulate a scene of two tasks

Scenario 1. Open App1 from launch in Task1, the root of the new Task2,task2 is activity_a, startup mode is default, Activity_a is opened from Activity_b., and then from Activity_ b Click Home back to launch, and then click App1, this time equivalent to direct Task2 from the background to the foreground, Task1 in the background, Task2 maintenance stack Activity position unchanged, Activity_b is still at the top of the stack, displayed in the front.

Scenario 2. App1 is opened from launch in Task1, the root of the new Task2,task2 is activity_a, the startup mode is Singletask, and Activity_a is opened from Activity_ B., then from Activity_b Click Home Back to launch, then click App1, this action will first determine whether Task2 root Activity is singletask, if yes, then execute activity_a Onnewintent () method, destory off activity_b. If you do not move the Task2 directly to the foreground display.

(Note: Scenario 2 If you do not click the icon from the launch to enter, but from the display of the recent tasks entered, it will not determine the root activity of the startup mode, will directly move the Task2 to the foreground.) The activity arrangement in Task2 is unchanged)

Simulate a scene of 3 tasks

Conditions: Task1 in a launch, Task2 in the App1 containing activity_a,activity_b, Task3 containing app2, activity_d, Activity_e.

Scenario 1: Enter Activity_a from launch, then go to Activity_b, open activity_e in normal mode in Activity_b (can be opened by an implicit start), and then in activity_e Opens Activity_f, although the Activity of another app is opened from Task2, but because it is open in the default mode, no new tasks are created Task3,activity_e and Activity_f are Task2, This time click home button back to launch, and then click App1, will Task2 move to the foreground, showing the Task2 at the top of the Activity_f, the most recent task will not appear Task3.

Scenario 2: enter activity_a from launch, then go to Activity_b, open Singletask in activity_e mode in Activity_b, and then activity_ E in open Activity_f, this time due to new task conditions, different app,singletask start, will create a new TASK3, and then open Activity_f in Activity_e, Then press the home key to enter into the launch, found the recent task appeared in Task2, and TASK3, this time click App1 found activity_b at the top, click App2 found that the TASK3 already exists, will not continue to create, display Activity_f is at the top, click the Back button from the Activity_f, go back to activity_e, then launch, and not go back to Activity_d, because Activity_d is not in the stack.

To summarize,

1: To create a new task, at least two conditions must be met: different apps and start in Singletask mode, otherwise open activity in the same app in Singletask will not create a new task. (There is a special case where setting the activity's Android:taskaffinity property allows the activity to appear to belong to another package, another activity)

2. Switching between multiple tasks in a recent task does not disrupt the arrangement of the activity in each task. However, when you click on the app icon for multiple task switching, the system will first determine whether the task root activity startup mode is Singletask, no, do not do anything, if so, execute the root activity of the Onnewintent method, Destory the activity that covers the root activity.

3. When a new task is not created, the newly opened activity will continue to be in the current task, regardless of which app it belongs to.

Analysis of the relationship between Android multi-task switching and activity startup mode Singletask

Related Article

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.