Android Launchmode load mode and intent flag

Source: Internet
Author: User

    • The difference and connection between applicationtask and process
      • Application
      • Task
      • Process
    • Four modes of loading
      • Standard mode
      • Singletop mode
      • Singletask mode
      • SingleInstance mode
    • Singletask and SingleInstance differences
    • Instance
    • Startactivityforresult If the activity loading mode is activated for Singletask and singleinstance
    • Using the intent Flag
      • Flag_activity_new_task
      • Flag_activity_single_top
      • Flag_activity_clear_top
      • Singletask and Flag_activity_new_task
      • General summary
        • Flag_activity_clear_top
        • Flag_activity_new_task
        • Flag_activity_no_history
        • Flag_activity_single_top
      • Main properties of activity
    • Handling Association Taskaffinity
      • Role
        • The Intent that initiates Activity contains the FLAG_ACTIVITY_NEW_TASK flag
        • Activity sets its Allowtaskreparenting property to True
    • The problem record I encountered
    • Good blog recommendation

The difference and connection between application,task and process

Task is a concept that exists in the framework layer and is easily confused with application (application) and process (processes).

Application

Application translated into Chinese is generally called "Application" or "Application", in Android, an application is a collection of components in general. As we all know, Android is a very high level of application-level system, the first lesson of Android development is to learn the four components of Android. When we have finished writing more than one component, and after registering these components in the manifest file, we can say that we have completed a application by packaging the resources used by these components and components into an apk. The relationship between application and components can be clearly reflected in the manifest file.

Task

Task is the concept of activity only when the program is running. Plainly, a task is a set of interrelated activity, a concept that exists in the framework layer, which controls the interface's jump and return. This task exists in a data structure called the back stack, which means that the framework manages user-opened activity as a stack. The basic behavior of this stack is that when the user jumps between multiple activities, the stack operation is performed and the stack is executed when the user presses the return key.

Tasks can be applied across applications, which is one of the important reasons why a task exists. Some activity, although not in the same app, but in order to maintain the consistency of user operations, put them in the same task. For example, clicking on a message in an activity A in our app launches an activity B of the mail program to send the message, and the two activity is in a different app, but is placed in a task so that when the message is sent, By pressing the back key, the user can return to the original activity A, which ensures the user experience.

Process

Process is generally translated into a process, which is a concept in the kernel of the operating system that represents the execution unit directly affected by the kernel dispatch. From an application perspective, the application we write in Java runs in a Dalvik virtual machine, and we can assume that a running Dalvik virtual machine instance occupies a process, so by default all components of an application run in the same process. However, there are exceptions to this situation, where different components in the application can run in different processes. You only need to specify the name of the process that the component is running in manifest by using the Process property

<activity android:name=".MyActivity" android:label="@string/app_nam"      android:process=":remote">  </activity>  
Four modes of loading

Android Activity Four startup mode Lunchmode the properties in the manifest configuration file are as follows:
(1) android:launchmode= "standard" (default)
(2) android:launchmode= "Singletop"
(3) android:launchmode= "Singletask"
(4) android:launchmode= "SingleInstance"

Standard mode

The activity's default load method, even if an activity already exists in the task stack, and another activity jumps to the activity via intent, will also create a new instance to press into the stack. For example: Now the stack is: a b c D, in D this activity through intent jump to D, then the current stack situation is: A B c D d. If D at the top of the stack jumps through intent to B, the stack is: A b C D d b. If you press the back key at this point, D D C B A will pop up the stack in turn and display it on the interface.

Singletop mode

(1) If an activity's launch mode is set to Singletop, then when the activity is at the top of the stack and then jumps through intent to the activity itself, it will not create a new instance to be pressed into the stack. For example, the case of the stack now is: A B C D. D launch mode is set to Singletop, then start in D intent jump to D, then will not create a new instance of D is pressed into the stack, the situation of the stack is still: A B C D. However, if the mode of B at this time is also singletop,d jump to B, then a new instance of B will be pressed into the stack, because B is not at the top of the stack, when the stack of the case becomes: a b C D B.

(2) An instance of an singletop Activity can be infinitely many, the only difference being that if there is already an activity instance of the same type at the top of the stack, intent will not create another activity, but onnewintent () is sent to the existing activity.
In singletop mode we need to process the intent sent in both OnCreate () and Onnewintent () to meet different situations.

(3) Application: One of the use cases of this startup mode is the search function. Suppose we create a search box that will navigate to a searchactivity that displays a list of search results, and for a better user experience, this search box will also be placed in searchactivity, so that users who want to search again do not need to press the back key.

(4) And when intent comes from another application, the new activity starts with the standard mode (Lollipop and later: A new task is created).

Singletask mode

If an activity is a singletask pattern, there will be only one instance of that activity in the task stack. For example, the case of the stack now is: A B C D. B's Launch mode is Singletask, at this time D through intent jump to B, then the case of the stack becomes: a B. and C and D are ejected and destroyed, which means that instances above B are destroyed.

Activity in the Singletask mode allows only one instance in the system. If there is already an instance in the system, the task holding the instance will move to the top and intent will be sent via Onnewintent (). If not, a new activity is created and placed in the appropriate task.

Once the intent is sent from another application and there are no instances of activity in the system, a new task is created and the new activity is created as the root activity. Suppose you already have an instance of Activity, no matter which task it is in (including the one above, in the application for this activity), the entire task will be moved to the top, and all the activity above the Singletask activity will be destroyed, The user needs to press the back key to traverse the activity in the play stack to return to the caller's task.

Scenario: The inbox of the mail client or the timeline of the social network. These activity is generally not designed to have multiple instances, Singletask can be satisfied. But it must be wise to use this mode, because some activity will be destroyed without the user's knowledge.

**1. The activity of the "Singletask" startup mode is set, and when it starts, it 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.
2. 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. **

Scenario: Activity of the browser
Reference: Mr. Luo's article is very deep, http://blog.csdn.net/luoshengyang/article/details/6714543

SingleInstance mode

Press the activity into a new task stack. For example: The case of Task Stack 1 is: A B C. C jumps to D via intent, and launch mode for D is singleinstance, a new task stack 2 will be created. At this point, the case of task stack 1 is still: A B C. The case for Task Stack 2 is: D. The screen interface displays the contents of D, and if D then jumps through intent to D, the task stack 2 does not create a new instance of D, so the case of the two stacks does not change. And if D jumps to C, then the case of stack 1 becomes: a b c C, because C's launch mode is standard, at this time if the return key is pressed, then stack 1 becomes: a b C. This means that the interface now also shows the contents of C, not d.

This pattern is very close to Singletask, where only one instance of activity is allowed to exist in the system. The difference is that there is only one activity in the task that holds the activity: the singleton itself.
But the result is very strange, from the information provided by Dumpsys, it seems that there are two tasks in the system but only one in the task manager, that is, the one that was finally moved to the top. Cause although there is a task in the background to run, we can not switch back, this is not scientific.
Because this task has only one activity, we can no longer cut back to Task # #. The only way to do this is to re-launch the app in Launcher.
There is a solution to this problem, as we did in Singletask acvity, as long as the taskaffinity attribute is set for singleinstance activity.

Usage scenario: Dialing the Phone interface

<activity            android:name=".SingleInstanceActivity"            android:label="singleInstance launchMode"            android:launchMode="singleInstance"            android:taskAffinity="">

This pattern is seldom used. Real-life cases such as launcher's activity or 100% determine only one activity application. In short, I don't recommend this mode unless it's absolutely necessary.

Reference: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0520/2897.html

Singletask and SingleInstance differences

Reference Link: http://blog.csdn.net/wang_zun_ren/article/details/6823257
Personal idea: Without specifying android:taskaffinity, the task Singletask exists is consistent with the application, and SingleInstance is a separate task, and cannot be returned to the interface, can only be reloaded. If other application B calls the Singletask interface B in app A, and if A->b->c,b calls B after the start, the a stack becomes a->b,c destroyed, click the Back button, go back to a, and know that a application exits and returns to the call page of App B.

Instance

Reference: http://blog.csdn.net/primer_programer/article/details/28849527

With this requirement, multiple tasks share an activity (singletask is sharing an activity in a task).
The ready-made example is Google Maps. For example, I have an application that is guided by the Google Maps activity which is called. Now, for example, by pressing the home button and then going to the app list to open Google Maps, you'll find that the map you just showed is actually the same activity.

This requirement cannot be achieved if the first three modes are used. There are several contextual activity in the Google Maps app, such as Route queries, and the Guide app has some contextual activity. Fallback in their respective apps to fall back into their context activity.

The singleinstance mode solves this problem (it took a long detour to get to the chase). Let the activity in this mode stand alone in a task stack. This stack has only one activity. Both the Guide app and the intent sent by the Google Maps app are received and displayed by this activity.

Here are two more questions:
If this is the case, multiple task stacks can also be considered an application. For example, the Guide app launches the map activity, which is actually created on the singleinstance mode above the Guide application task stack (if there is no, if there is a direct display of it) a new stack, when the only activity in the stack, Map activity back when, just put this stack away, so you see the guide application just the activity;
Multiple applications (tasks) sharing an activity requires that none of these apps exit, such as the emphasis on using the home button to switch from the Guide app to the map app. Because if you exit the Guide app and the map app is not running, the individual map activity (task) exits.

Startactivityforresult If the activity loading mode is activated for Singletask and singleinstance

Activated activity load mode Singletask, get not the return value, 0 is result_canceled, that is, the direct cancellation, Onactivityresult received result_canceled
SingleInstance, he is similar to Singletask, the difference is that singleinstance requires only one instance of activity, and there is only one activity instance in the whole task, Other activity instances are allowed in the stack where Singletask is located.

As a result, we can know that the activity that set up the singleinstance also has the above features, in addition, the activation activity set the singleinstance, then regardless of the activity that is initiated has no setting singleinstance , you cannot get the return value

Using the intent Flag

Reference: https://developer.android.com/guide/components/tasks-and-back-stack.html

When you start activity, you can modify the default association of activity and its tasks by adding the appropriate flag to the Intent passed to StartActivity (). Flags that you can use to modify the default behavior include:

Flag_activity_new_task

Start Activity in a new task. If a task is already running for the Activity that is being started, the task will go to the foreground and revert to its final state, and the activity will receive a new Intent in Onnewintent ().
As mentioned earlier, this produces the same behavior as the "Singletask" Launchmode value.

Flag_activity_single_top

If the activity being started is the current activity (at the top of the return stack), the existing instance receives a call to Onnewintent () instead of creating a new instance of the activity.
As mentioned earlier, this produces the same behavior as the "Singletop" Launchmode value.

Flag_activity_clear_top

If the activity that is being started is already running in the current task , all activity at the top of the current task is destroyed, and this Intent is passed to the activity's recovered instance (now at the top) through Onnewintent (). Instead of starting a new instance of the Activity.
The Launchmode property that produces this behavior has no value.

Flag_activity_clear_top are usually used in conjunction with Flag_activity_new_task. When used together, these flags allow you to find existing Activity in other tasksand place it in a location where you can respond to Intent.

Singletask and Flag_activity_new_task

Reference: http://www.2cto.com/kf/201410/347854.html

When you set the
Android:launchmode= "Singletask"
, the system will find the corresponding taskaffinity task space has started the activity, if not started, in this taskaffinity task space to start, if there is a boot, then push the activity to the top of the stack, Then the activity above this activity is pushed out of the stack, not in task tasks, that is, OnDestroy.

General summary

Common flag parameters for intent:

Flag_activity_clear_top:

For example, the current stack situation is: A B C D. D jumps through intent to B at this point, if the intent adds flag_activity_clear_top tag, the stack becomes: A B. If you do not add this tag, the stack will become: A b C D B. That is, if the flag_activity_clear_top tag is added and the target activity already exists in the stack, the activity on top of that target activity will be ejected from the stack for destruction. This is similar to setting the launch mode of B to Singletask.

Flag_activity_new_task:

For example, the case of stack 1 now is: A B C. C jumps to D through intent, and this intent adds a flag_activity_new_task tag, and if D this ACTIVITY adds a TASK affinity to the declaration in Manifest.xml, And unlike the affinity of stack 1, the system will first look for a task stack with the same task affinity as D, if it exists, press D into that stack, and if it does not, it will press it into the stack of a new D affinity. If the TASK affinity of D is not set by default, or is the same as the affinity of stack 1, it will be pressed into the stack 1 and become: a B C D, so it is the same as the non-flag_activity_new_task tag effect. Note If you attempt to start an activity from a non-activity, such as starting an activity from a service, intent for example to add a flag_activity_new_task tag .

Flag_activity_no_history:

For example, the current stack is: A B C. C Jump to D through intent, this intent add flag_activity_no_history flag, then the interface displays the contents of D, but it does not press into the stack. If you press the back key, return to C, the case of the stack is: A B C. If you jump to E in D at this point, the stack becomes: A B C E, at which point the return key will return to C, because D is not pressed into the stack at all.

Flag_activity_single_top:

Similar to the Launch mode singletop above activity. If a intent adds this flag, and the target activity of this intent is the activity at the top of the stack, then a new instance will not be pressed into the stack.

Main properties of activity:

Allowtaskreparenting: When set to True, the flag_activity_new_task tag is similar to intent.
Alwaysretaintaskstat: If a user moves a task into the background for a long time, the system pops up the stack content of the task and leaves only the activity at the bottom of the stack, where the user returns and sees only the root activity. If this property of the activity at the bottom of the stack is set to true, this behavior is blocked, preserving all stack content.
Cleartaskonlaunch: This property of root activity is set to true, and the property of the alwaysretaintaskstat above is true instead.
Finishontasklaunch: For any activity, if its property is set to true, the activity will not exist when the task is placed in the background and then restarted.

Handling Association Taskaffinity

Association indicates which task the Activity first belongs to. By default, all Activity in the same app is associated with each other. Therefore, by default, all Activity in the same app takes precedence in the same task. However, you can modify the Activity's default association. Activity defined in different apps can share associations, or you can assign different task associations to activity defined in the same app.
You can use the taskaffinity property of an element to modify the association of any given Activity.

The Taskaffinity property takes a string value that must be different from the default package name declared in the element because the system uses that name to identify the default task association for the app .

The Intent that function initiates Activity contains the FLAG_ACTIVITY_NEW_TASK flag.

By default, new activity is initiated into the activity task that calls StartActivity (). It pushes into the same return stack as the caller. However, if the Intent that is passed to StartActivity () contains the FLAG_ACTIVITY_NEW_TASK flag, the system will look for additional tasks to store the new ACTIVITY. This is usually a new task, but does not make a mandatory requirement. If an existing task has the same association with the new activity, the activity is initiated into the task. Otherwise, a new task will start.
If this flag causes Activity to start a new task and the user leaves by pressing the home button, you must provide the user with a way to navigate back to the task. Some entities, such as the Notification Manager, always start activity in an external task and never start activity as part of themselves, so they always put Flag_activity_new_task into Intent that is passed to StartActivity (). In Note that if the Activity can be called by an external entity that can use this flag, the user can return to the started task independently, for example, using the launcher icon (the root Activity of the task has the Category_launcher Intent filter ; See the Startup Tasks section below).

Activity sets its Allowtaskreparenting property to "true".

In this case, the activity can move from the task it initiates to the task with which it is associated (if the task appears in the foreground).
For example, suppose that the Activity that reports the weather condition of the selected city is defined as part of the travel application. It has the same association (default app affinity) as other Activity in the same app, and allows the parent to be reset with this property. When one of your activity initiates the weather forecast activity, it initially belongs to the same task as your activity. However, when a travel app's task appears in the foreground, the forecast Activity is reassigned to the task and displayed in it.

The problem record I encountered

1, Android Press the home button to return to the desktop, and then press the Desktop app icon and reopen the solution for the app.
Workaround: This problem is because in Androidmanifest.xml, your startup activity adds android:launchmode= "Singletask" to remove this line of code.
Reference: http://bbs.csdn.net/topics/350051074
And: http://blog.csdn.net/luohai859/article/details/47784317
ALSO: https://www.zhihu.com/question/32775665

Good blog recommendation

http://blog.csdn.net/zhangjg_blog/article/details/10923643
Http://www.2cto.com/kf/201410/347854.html

Android Launchmode load mode and intent flag

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.