Task stack activity start mode flag taskaffinity in intent

Source: Internet
Author: User
Tags exit in


about task Stack
task
 stack   is a common data structure that allows access to only the elements on top of the stack, which is like a cup, each time it can only take something on top of the Cup, For stacks, you can only access the top elements of the stack each time, so that other elements below the top element of the stack are protected. "Advanced after-out" or "LIFO" is a big feature of the stack, the first step into the stack of elements will always wait until after the elements into the stack after the stack can be out of the stack. Recursion is the use of the system stack, temporarily save temporary results, to protect the temporary results. The basic operation of the  stack: stack, stack task stack tasks are simply a collection of activity components that are clustered together in a stack of patterns, similar to a container filled with activity, the first activity to join is at the bottom of the container, The last addition is at the top of the container, and when the activity is removed from the task, it is removed from the top, and the last one is the first to add activity, which is the LIFO  

activation mode of activity
Activity startModeThere are four kinds,you can set up a corresponding startup mode for your activity according to your actual needs, so you can avoid creating a large number of repetitive activity issues. To set the activity's startup mode, just set the Android:launchmode property in the corresponding <activity> tag in Androidmanifest.xml. Standard
The default mode, which can be configured without writing. In this mode, a new instance will be created by default. Therefore, in this mode, you can have multiple identical instances, and also allow multiple identical activity overlays. For example:If I have an activity named A1, there is a button to jump to A1. Then if I click on the button, it will start a new activity A1 stack on the A1 just above, then click, and then City one on it. Click the back key to exit in the order of the stack. SingletopThere can be multiple instances, but multiple identical activity overlays are not allowed. That is, if the activity is at the top of the stack, the same activity is started, the new instance is not created, and its Onnewintent method is called.Singletask
There is only one instance. If you launch it in the same application, if the activity does not exist, a new instance is created in the current task and, if present, the other activity on top of the task is destory off and the Onnewintent method is called. If you start it in another application, a new task is created and the activity is started in that task. Singletask allows other activity to be stored in a task, that is, if I open a new activity in this Singletask instance, the new activity will be in the task of the Singletask instance. can useSingletaskto exit the entire application. set the main activity to Singtask mode, then transfer the activity to the main activity in order to exit, then rewrite the onnewintent function of the main activity and add a finish to the function.
singleinstance
There is only one instance, and this instance runs independently in a task, and this task is the only instance that does not allow any other activity to exist.

flags commonly used in the intent
Flag indicates the intent, which is commonly used in the activity scene, and is closely related to the activation mode of the activity. The following is an enumeration of the flag properties associated with the activity startup mode:Intent.flag_activity_new_taskThe system checks to see if any of the currently created tasks have a task for the activity to be started, and if so, creates an activity on the task, and if not, creates a new task with the activity property, creating the activity on the newly created task.
Flag_activity_single_topThis flag is equivalent to the singletop in the boot mode, such as the original stack is ABCD, start D in D, the case in the stack or ABCDFlag_activity_clear_topThis flag is equivalent to the singletask in the boot mode, and this flag-activated activity will pop up the stack space on the activity above the activity to be started. For example: The situation in the original stack is ABCD, this time jump from D to B, this time the stack is the case of AB flag_activity_no_historyThis tag, as the name implies, means that the activity that is activated with this flag will not exist in the task stack once it is "invisible". For example, the original is ABC, this time in C with this flag to start D, D and then start E, this time the stack in the case of ABCE. Flag_activity_brought_to_front Let's say I'm starting B in a, and the intent in a in a starts with this tag, and then start the CD in B (normal boot CD), and the stack isABCD. If you start B again at this time in D, the case of the stack is ACDB. In particular, I said that the online description of this flag, it will be very easy to misunderstand this, a,b,c,d are standard loading, and then I start a in D, this intent plus flag_activity_brought_to_front, will be mistaken to become B , c,d,a!! Actually not, this time should be a,b,c,d,a. People who don't believe, try it. But the following mark and this mark will make everyone understand! Flag_activity_reorder_to_front as Intent.flag_activity_brought_to_front finally said, if the a,b,c,d normal start, whether B has no use flag_ Activity_brought_to_front start, this time in D in the start of B, or will become a,c,d,b. Flag_activity_reset_task_if_needed This also do not know how to use, look at the document a bit like to open up a new TASK, but the test, not as simple as that. Flag_activity_no_user_actiononuserleavehint (), as part of the activity cycle, is used when the activity is returned to background because the user wants to jump to another activity. For example, when the user presses the home key (the user's choice), it will be called. For example, if a phone comes in (not a user's choice), it will not be called. So how does the system differentiate between making the current activity go back to background and using the user's choice? It is determined by whether there is flag_activity_no_user_action in the intent of the newly activated activity that prompted the current activity to retreat to background. Note: This function is not called when the activity is destroyed by calling finish ()

all flags defined in the intent
   flag_grant_read_uri_permission // flag_grant_write_uri_permission // flag_from_background // flag_debug_log_resolution // flag_exclude_stopped_packages // flag_include_stopped_packages // flag_activity_no_history //exit will not exist in the task stack  flag_activity_single_top //single Top  flag_activity_new_task //Open in new task stack flag_activity_multiple_task //  flag_activity_clear_top //If this activity already exists in the task stack, it pops up all of its top activity when it starts again flag_activity_forward_result // flag_activity_previous_is_top // flag_activity_exclude_from_recents // Flag_activity_brought_to_front // flag_activity_reset_task_if_needed // flag_activity_launched_from_history // flag_activity_clear_when_task_reset // flag_activity_no_user_action // Flag_activity_reorder_to_front // flag_activity_no_animation // flag_activity_clear_task // flag_activity_task_on_home // flag_receiver_registered_only // flag_receiver_replace_pending // flag_receiver_foreground // flag_receiver_registered_only_before_boot // flag_receiver_boot_upgrade // 

about the Taskaffinity property
The properties of the activity in the manifest fileandroid:allowtaskreparentingfor setting:activity is No ability to move from the task stack that started it to another task stack that has the same Taskaffinity property value as the task stack  that started it , the timing of the transfer was in anotherTask Stackwhen I was brought to the front desk. If set to true, it can be transferred, and if False is set, the activity must be kept at the one that started it.Task Stackthe. 
Experiment1, new two projects, App1 and APP2APP1 and APP2 are set android:taskaffinity= "Aaa.aaa" android:allowtaskreparenting= "true" first run APP1, Then click the Home button, let App1 run in the background and then run APP2, will find that this is the App1 mainactivity, and long press the home key, will find that the program has been run only App1.2, followed by another experiment on this basisCreate a new secondactivity on APP1, set android:taskaffinity= "AAA.BBB" android:allowtaskreparenting= "true" When startactivity in Mainactivity, set the Flag property in intent to flag_activity_new_task, note that the flag property here must be newtask. Then run APP1, click Enter Secondactivity, then long press the Home button, you will find that the program has been running two App1, and one of the display is mainactivity another show is secondactivity. At this point, click the Home button, let the program back to the background and then run APP2, you will find that the display is App1 mainactivity, when the click Back will return home directly. Note, although at this time App1 Mainactivity was finish, but App1 secondactivity still in the backstage waiting. Similarly, long press the home key, you will find that the running program only App1 two task stacks, and there is no App2. 3, on this basis heap App1 again modified on APP1 to create a new thirdactivity, set the property android:taskaffinity= "Aaa.aaa" android:allowtaskreparenting= "true" And when startactivity in Secondactivity, set the Flag property in intent to Flag_activity_new_task; run App1, click into secondactivity, Then enter the thirdactivity, and then long press the home key, you will find that the program has been running two App1, and one of the display is secondactivity another is thirdactivity. When you click Back, you will return to Mainactivity, then click Back, return to Secondactivity, and then click Back to return to the home page. In the above experiment, if StartActivity does not set the flag attribute in intent to flag_activity_new_task, then if the first start is APP1, Then either the activity in the APP1 or the mainactivity in the APP2 is a task stack running APP1.



From for notes (Wiz)

Task stack activity start mode flag taskaffinity in intent

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.