Detailed startup mode for activity in Android programming

Source: Internet
Author: User

Activity has four modes of loading: standard (default), Singletop, Singletask, and SingleInstance. Refer to the official documentation, which shows the differences below:

The default load method of the standard:activity, even if an activity already exists in the task stack, and another activity jumps to the activity through intent, the same will 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: 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 the intent to itself, the activity will not be created with a new instance 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.

Singletask: If an activity is Singletask mode, 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.

SingleInstance: Pushes 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.

Now there is a problem in this case if the user clicked the Home button, then will not be back to the instant interface of D. If you want to solve this problem, you can add the declaration of D in the Manifest.xml file

<intent-filter>

<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>

</intent-filter>

Add this paragraph, that is, there are two such declarations in the program, and the other is the normal root activity, after the APK package installed, in the program list can see two icons, but if all run, in the Task Manager in fact there is only one. In the above case, click on the icon of D to return to its immediate interface (such as a edittext, the previously entered content, and now back to exist).

Corresponding to this is the flag of intent, commonly used flag parameters:

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, now the stack condition 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 singletop of Launch mode 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.

Detailed startup mode for activity in Android programming

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.