android-boot mode task-lunchmodle-intent flag summary

Source: Internet
Author: User


Summarize:
    • Activity within the same task can be an activity from a different process
    • Activity within the stack is not reordered, only push or pop
    • The standard mode allows multiple instances to be used in different task
    • There is only one instance of Singletask activity
    • Singletask activity If a separate Taskaffinity property value is set, it will be in a new task when it is started, or it will be in an existing task
    • When the activity of Singletask is started, it will see whether the corresponding activity instance already exists in the target task (new task or existing task), and if so, The activity on the activity instance will be destroyed (pop, destroy), that is, the activity instance will end up on the top of the task's stack.
Task

Task is a concept from the user's point of view, which is a combination of activities that are combined to allow the user to complete a work (or operation).

The activity within the task is organized in stacks, which is the back stack. Activity within the stack is not reordered, only push or pop. The activity within the stack can come from different apps, so it can be run in a different process, but they all belong to the same task.

The Android system is a real-time multi-task system that allows users to switch between multiple tasks at will. When all activity in a task stack pops, the task is destroyed. Sometimes the system destroys the activity in order to reclaim memory, but the task is not destroyed.

Activity has the Launchmode option in manifest and can configure its startup mode. In standard mode, one activity is allowed to exist in multiple instances simultaneously, either in the same task or in a different task. Manifest can specify that the launchmode,intent can specify intent flag, which is used by the initiator, which is used by the initiator, and which overrides the former when used.

Launchmode
  • Standard, default mode, allows multiple instances
  • Singletop, when there is a new boot request compared to standard, it is called without creating a new instance until the target activity is at the top of the current stack, onNewIntent() and other conditions are consistent with standard
  • Singletask, (this excerpt from Lao Luo's blog) sets the activity of the "Singletask" startup mode, which, when started, finds the property value in the system affinity equals its property value taskaffinity the task exists If there is such a task, it will start in this task, otherwise it will start in the new task. Therefore, if we want to set the activity of the "Singletask" startup mode to start in a new task, set a separate Taskaffinity property value for it. If the activity that has the "Singletask" startup mode set is not started in a new task, it will see if there is already an activity instance in the existing task, and if so, The activity that is located above the activity instance is ended, that is, the activity instance will end up on the top of the task stack.
  • SingleInstance, compared to Singletask, the difference is that the task of singleinstance activity will only have this activity
  • Back to Navigation: Singletask and SingleInstance start the activity, although may not be the same task, but will still return to the original activity, but Singletask may exist back stack "stitching" situation
Launchmode Verification Test

The main test is the Singletask attribute, the other document descriptions are relatively clear, and it is not easy to misunderstand. Two groups of experiments, not setting taskaffinity and setting taskaffinity, singletask behavior. Compare the two "recent tasks", Dumpsys,logcat, and return the differences in navigation.

Test 1

Not set taskaffinity,mainactivity, Singletaskfirstactivity, simpleactivity, Singletasksecondactivity, Select the picture intent, then return all the way.

Only one Taskdemo task can be seen in the recent task, and the results of Dumpsys are as follows:

All activity is within the same task, validating Lao Luo's conclusion. Note also that the two places of the red, their Processrecord objects, imagegallery and singletaskactivity in different processes, but also verify that the "can be run in different processes" described earlier.

The Logcat log is as follows:

All the way back to Singletasksecondactivity, simpleactivity, singletaskfirstactivity,mainactivity, desktop.

Test 2

Set the taskaffinity for singletaskfirstactivity to a separate value, Mainactivity-singletaskfirstactivity.

As you can see from recent missions, there are two taskdemo tasks. The results of Dumpsys are as follows:

As you can see, mainactivity and Singletaskfirstactivity run in two different tasks (t225 at the bottom of the mainactivity, singletaskfirstactivity at the bottom of the stack) , but they still belong to the same process.

We then started simpleactivity, then switched back to t225 through the recent task, and also started simpleactivity, when the results were dumpsys as follows:

Simpleactivity have two instances at the same time, their hashcode is different, currently in resumed state is the instance in t225.

And by looking at Logcat:

We also found that Simpleactivity was created with two instances.

At this point we switch back to t226 through the recent task, and start singletasksecondactivity here, view Dumpsys, and then switch back to t225 and start singletasksecondactivity, View Dumpsys and Logcat:

We can see that although the first boot singletasksecondactivity is in t226, but the singletasksecondactivity instance is running in t225, why? Because Singletasksecondactivity does not set the Taskaffinity property, it and Mainactivity will run in the same task! And Lao Luo's analysis is consistent. The second startup, instead of creating a new Singletasksecondactivity instance, calls its Onnewintent method, which is consistent with the document description.

Now let's test the return navigation, press the back key in turn, observe each return activity, and the results of each dumpsys, as well as the results of Logcat:

For the first time, I returned to T225 's Simpleactivity,dumpsys:

The second time back to T225 's Mainactivity,dumpsys:

The third time returned to T226 's Simpleactivity,dumpsys:

The fourth time returned to T226 's Singletaskfirstactivity,dumpsys:

The last return returns to the desktop.

The Logcat log is as follows:

From the return path above, it does validate the description of the "stitching" of the back stack on the developer document, i.e., not returning in the order of most recent first, but prioritizing a task's back stack (t225) After the pop is finished, pop the next task's back stack (t226).

Test 3

Based on the version of Test 2, mainactivity, singletaskfirstactivity, simpleactivity, then switch back to Mainactivity, Start singletaskfirstactivity again.

To view recent tasks, there are still two task, but view Dumpsys and Logcat:

We can see that the first boot of the simpleactivity is pop (destroy), so that the singletaskfirstactivity is not at the top of the stack to mention the top of the stack. This also verifies that the activity within the back stack will not be reordered, the most pop and push facts.

Intent Flag

As long as you figure out what's easy to misunderstand in Launchmode, for intent flag, just remember a little bit: intent flag can override the Launchmode settings. In order to avoid lengthy content, this article does not conduct a detailed test analysis of intent flag.

Summarize
    • Activity within the same task can be an activity from a different process
    • Activity within the stack is not reordered, only push or pop
    • The standard mode allows multiple instances to be used in different task
    • There is only one instance of Singletask activity
    • Singletask activity If a separate Taskaffinity property value is set, it will be in a new task when it is started, or it will be in an existing task
    • When the activity of Singletask is started, it will see whether the corresponding activity instance already exists in the target task (new task or existing task), and if so, The activity on the activity instance will be destroyed (pop, destroy), that is, the activity instance will end up on the top of the task's stack.

android-boot mode task-lunchmodle-intent flag summary

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.