Sissy said
As an Android small white, or willing to share my exploration journey with you, then let us first a little from Android Launchmode warm tips ~ (≧▽≦)/~.
Background knowledge
“现在我的手中有一摞牌,但是我让你看到的只有一张。”
“其他牌去哪了呢?”
“其他张牌都被最上面这张牌挡住了呀。”
The above paragraph is a very vivid explanation of the relationship between the acitivity and the task stack.
Next, let's look at the task--task first.
Task
- Task is a collection of activities, which is managed by the back stack, and relies on the advanced post-out queue.
- There is at least one activity in each task, and the activity of the new instance is placed on top of the stack
- Task can be switched to the background
Taskaffinity
- Taskaffinity This attribute primarily determines which task each activity belongs to.
- By default, activity in the same package shares the same affinity (task sharing).
Launchmode
- Used to set how the activity should start
- There are four main modes:
- Standard (default)
- Singletop
- Singletask
- SingleInstance
Is there a situation where you need to change the default settings pinch?
- There are multiple portals pointing to the app
- Launcher
- Notification
- Share Intent
- "Single-Case" Activity
- For example, a browser wants to share data with many instances.
- What can be changed?
- Launchmode property of activity
- Flag of Intent
In these cases, our default boot mode is not sufficient.
So here's the problem. Boot mode which strong ([email protected][email protected]=)?
Next, let's get closer to Android's 4 startup modes ~
Standard
- Activity in this loading mode can have multiple instances;
- Its instances can reside in different tasks;
- A task can have multiple instances of it
Come on, let's do an experiment together.
- Experiment Configuration:
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android" package ="Com.xixi.standardlaunchmode" > <applicationandroid:allowbackup="true"android:icon="@ Mipmap/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme " > <activityandroid:name=". Activitya "android:label=" @string/app_name " /> <activityandroid:name=". Activityb "android:taskaffinity=" Com.other " /> </Application></manifest>
Experimental process
Experiment Analysis:
- How do you draw the pinch of this picture?
- First, we can check the log first.
- Next, we'll use "adb shell Dumpsys" to detect the Android activity task stack.
??
We can see that the standard mode of the two activity is constantly instantiated at each start, but we know Activityb taskaffinity is "Com.other" ah, why does not work pinch? ~
- Because we did not add parameters for intent: Flag_activity_new_task, plus will not work in the future pinch?
What kind of a diary is the pinch?
- Performance Summary:
- Before Lollipop, the activity is created for each intent, and the new activity is placed in the same task stack that emits the intent activity (we do not consider the taskaffinity difference);
- In the lollipop version, the activity from the same application, with the same effect as before, was created to be placed on top of the stack, but if it was a different application activity, The activity that is created is placed in a new stack (see also: http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en)
Singletop
- In essence, this pattern is not much different from standard mode, except in a special case.
- When an instance of the schema activity already exists on the stack top of the back stack, it is not re-instantiated, but instead invokes the onnewintent () instance
Come on, let's do an experiment together and demonstrate the following:
- Experiment Configuration:
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android" package ="Com.xixi.standardlaunchmode" > <applicationandroid:allowbackup="true"android:icon="@ Mipmap/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme " > <activityandroid:name=". Activitya "android:label=" @string/app_name " /> <activityandroid:name=". Activityb "android:taskaffinity=" Com.other " /> </Application></manifest>
- Experimental process:
- Experiment Analysis:
First we still look at the log:
Next, look further down the task stack:
Performance Summary
- Basically the same as standard, only if the task stack already exists, you do not need to re-create, but call Onnewintent ()
Next, we need to go into a completely different way from the first two modes, take a break:
Av8d,r U ready? Hey, it's not enough!
Singletask
For this torture "goblin", this is described in the official documentation:
The system creates a new task and instantiates the activity at the root of the new task.
However, if an instance of the activity already exists in a separate task, '
The system routes the intent to the existing instance through a call to its onnewintent () method,rather than creating a NE W instance. Only one instance of the activity can exist at a time.
and the truth is , is it the first experiment?
Experiment 1: The start mode of a is set to Singletask 3 activity is in the same task
- Experiment Configuration:
<activity android:name = ". Activitya " android:launchmode =" SingleTask " /> <activity android:name =/> <activity android:name =". Activityc " android:taskaffinity =" com.others "/>
- Experimental process Recording
We can see that when you start a again, because an instance of a is already present in the task stack, the other already existing activity pops out in order to refer to the top of the stack.
- Experiment Log Task Stack changes:
After starting a again:
Log:
So, what happens if B is singletask a?
Experimental 2:b start mode is set to Singletask 3 activity are in the same task
- Experiment Configuration:
<activity android:name = ". Activitya "/> <activity android:name =". Activityb " android:launchmode =" SingleTask " /> <activity android:name = android:taskaffinity =" com.others "/>
Experiment 3: A's start mode is set to Singletask 3 activity is not in the same task
In this experiment, we found that Activitya was still in task 25 and was not instantiated in the new task, and when it was started again, the activity on top of it was ended in order to put it on the stack.
- Performance Summary
- Activity is singletask start mode, there are only 1 instances;
- If the activity in the same task wants to start it again, the activity on top of it in the stack will be all over, so that it is at the top of the stack of the task stack;
- If you want to make it start in a new task, you need to set the Taskaffinity property for it
SingleInstance
Same as Singletask with only one instance so what's the difference?
Experiment 1: The start mode of a is set to SingleInstance 3 activity is in a different task
- Experiment Configuration:
<activity android:name=".ActivityA" android:launchMode="singleInstance" /><activity android:name=".ActivityB" /><activity android:name=".ActivityC" android:taskAffinity="com.others" />
The startup mode of the experimental 2:b is set to SingleInstance 2 activity in a different task
- Experiment Configuration:
<activity android:name = ". Activitya "/> <activity android:name =". Activityb " android:launchmode =" SingleInstance "/> <activity android:name = android:taskaffinity =" com.others "/>
Summarize
- Android Launchmode:
- There are many instances of standard:activity, and different instances can be located in different tasks;
- Singletop:activity is the same as standard, only when the top of the stack has its instance, there is no need to instantiate it;
- Singletask:activity has only 1 instances, and other activity emits intent need to start it, and it is in the same stack of activity will be ended, so that it is located on the top of the stack;
- SingleInstance: Very much like singletask, but it is more aloof, that is, there is only one task in the system that holds it, and this task only stores it, and it has only one.
Access information
Http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack
Http://developer.android.com/guide/components/tasks-and-back-stack.html
http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
There is also a Chinese version of the HA acitivity start mode graphics
http://blog.csdn.net/guolin_blog/article/details/41087993
http://blog.csdn.net/luoshengyang/article/details/6714543
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Warm tips from Android Launchmode