An analysis of "android in depth" activity startup mode
Source: Internet
Author: User
<span id="Label3"></p><p><p><span class="corner"><span class="corner">Abstract activity startup mode is a basic and easy to ignore problems, but this problem has a deep understanding of the programmer to write a stable and efficient Android program to help a lot, today, under the guidance of b, we on the activity start mode, Intent Flags did a good job exploring, so to speak, if you are unfamiliar with or understand the Activity's startup mode or how to use flags, in the future you will definitely be troubled in the actual development, back to re-learn this section, for example: someone wrote the client, why crashed, There is still one or even N of the Application's interface, if you are familiar with and understand this chapter accurately, it will inevitably not make this error.<br></span></span></p></p><p><p><span id="AnchorContentToggle" class="corner" title="点击收起目录">Catalogue [-]</span></p></p> <ul> <ul> <li class="osc_h1">Objective:</li> <li class="osc_h1">Inquiry History:</li> </ul> </ul><strong><strong>preface:</strong></strong><p><p>Activity startup mode is a basic and easy to ignore problems, but this problem has a deep understanding of the programmer to write a stable and efficient Android program to help a lot, today, under the guidance of b, we on the activity start mode, Intent Flags did a good job exploring, so to speak, if you are unfamiliar with or understand the Activity's startup mode or how to use flags, in the future you will definitely be troubled in the actual development, back to re-learn this section, for example: someone wrote the client, why crashed, There is still one or even N of the Application's interface, if you are familiar with and understand this chapter accurately, it will inevitably not make this error.</p></p><strong><strong>Inquiry History:</strong></strong><p><p>① <strong>What is a stack</strong></p></p><p><p>② <strong>Activity Stack</strong></p></p><p><p>③ <strong>Task</strong></p></p><p><p>④ <strong>activity startup mode</strong></p></p><p><p>⑤ <strong>activity stack and task contact</strong></p></p><p><p>⑥ <strong>Intent Flags</strong></p></p><p><p>⑦ <strong>activity-related Properties Taskaffinity</strong></p></p><p><p><strong>1.</strong> <strong>What is a stack</strong></p></p><p><p>1.1 <strong>Stack</strong></p></p><p><p>Stack is a commonly used data structure, the stack only allows access to the top of the stack of elements, the stack is like a bullet shuttle (), each time can only take the top of the stack, and for the stack can only access its top elements of the stack each time, so as to achieve the protection of the top elements of the stack below the other Elements. "advanced after-out" or "lifo" is a big feature of the stack, the elements of the advanced stack will always wait until the elements of the backward 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.</p></p><p><p></p></p><p><p>1.2 <strong>definition Stack (stack)</strong></p></p><p><p>A stack is a linear table that restricts the insertion and deletion of operations only at one end of the Table.</p></p><p><p>(1) the end of the insert, delete is usually called the top of the stack (top), the other end is called the bottom of the stack (Bottom).</p></p><p><p>(2) when there are no elements in the table, it is called an empty stack.</p></p><p><p>(3) the stack is a linear table of last-in, first-out, or LIFO table. Stack modification is based on the principle of Last-in-first-out. Each deletion (fallback) is always the "newest" element in the current stack, which is the last element to insert (the stack), and the first inserted is placed at the bottom of the stack, to be ejected at the End.</p></p><p><p></p></p><p><p>1.3 <strong>Stack Operation</strong> : press stack, Stack</p></p><p><p></p></p><p><p><strong>2.</strong> <strong>the stack in the</strong> Activity</p></p><p><p>Android management is mainly through the activity stack, when an activity starts, the system will press it into a specific stack according to its configuration, the system is in a running State. When the user clicks back or finish () the activity, then it will be pressed out of the stack, then destroyed, according to the activity of the life cycle can be known, If the activity in the currently displayed stack is not popped (that is, the Ondestory method that invokes activity), a new activity is opened by intent, and the newly opened activity is pushed to the top of the Stack. The following figure (1)</p></p><p><p></p></p><p><p></p></p><p><p>Figure (1)</p></p><p><p><strong>3. Task</strong></p></p><p><p>Task is the activity stack, which is simply a set of activity components gathered together in a stack of patterns to record the behavior of the User. (just mention it and the Activity's startup Mode)</p></p><p><p></p></p><p><p><strong>4. Activity</strong> <strong>startup mode</strong></p></p><p><p>Property: Android:launchmode</p></p><p><p>Role: the Activity's Launchmode property in the main profile <em>Android</em>manifest.xml determines how the activity starts.</p></p><p><p>Description: There are four modes that work with the properties of the activity flags in the intent object (the flag_activity_* Variable) to determine how the activity starts to handle Intent.</p></p><p><p>Four types of modes:</p></p><p><p>① "<strong></strong>standard"--default mode<br>② "<strong>singletop</strong>"<br>③ "<strong>singletask</strong>"<br>④ "<strong>singleinstance</strong>"</p></p><p><p>The following examples illustrate their differences:</p></p><p><p><strong>A. </strong> <strong></strong>standard<strong>:</strong></p></p><p><p>The Activity's default load method, which initiates the creation of a new activity instance, pushes the instance into the stack (regardless of whether the activity already exists in the task stack, takes a new action), and invokes the new Activity's OnCreate () method:</p></p><p><p>For example: the order in the stack is a B c d, at this time D through intent jump to a, then the stack structure becomes a B c D a, Click the return button to display the order is D C B a, in turn destroy. such as (2)</p></p><p><p>Word memory: regardless of whether the stack has been created, it will create a new one and put it on top of the stack and display it, calling the OnCreate method.</p></p><p><p>Usage scenario: The default Activity's startup Mode.</p></p><p><p></p></p><p><p>Figure (2)</p></p><p><p><strong>B. </strong> <strong>singletop</strong><strong>:</strong></p></p><p><p>Assuming a B C D already exists in the stack</p></p><p><p>Singletop mode, The following two cases are Divided.</p></p><p><p>1) The current app show is D or the top of the stack is d, if this time through intent need to open d, then will not re-create a new D instance, but call the top of the stack d, and call its Onnewintent method (note is not the OnCreate method, The OnCreate method is only invoked when a new instance is created, so the structure in the stack is still a B C D. such as (3)</p></p><p><p></p></p><p><p>Figure (3)</p></p><p><p>Think: well, Let's think about when this happens. Seems a bit contradictory, the current display or the top of the activity, through intent or open the same activity, there is this use and necessary?</p></p><p><p>As the old saying goes: all things have its law, Android provides this usage, then there must be a reason, as to how to apply and how to apply, then to be verified by the reader, here we just offer, but can provide a scenario to demonstrate its effectiveness.</p></p><p><p>For example: the current app stack top is a, this time, suddenly came a message notification, this message needs to open a through intent, then we click on this message to open a, This scene is reproduced, it will not create a new a, but the reuse of a stack top, and execute the onnewintent method.</p></p><p><p>2) The current app show is D or the top of the stack is d, if you need to open B at this time, because B is not at the top of the stack, so a new B instance will be created and pressed into the stack, and call the new OnCreate method to create b, the structure becomes a B C D B. such as (4)</p></p><p><p></p></p><p><p>Figure (4)</p></p><p><p>A word memory: only need to open a at the top of the stack, then do not create a new a, and call the Onnewintent method, if the need to open a is not on the top of the stack, then whether a in the stack or not, will create a new a put on the top of the stack, and execute the OnCreate method.</p></p><p><p><strong>C. </strong> <strong>singletask</strong><strong>:</strong></p></p><p><p>In Singletask mode, There is only one instance of the task stack that corresponds to the Activity.</p></p><p><p>For example, the structure of the current stack is: A B C D.</p></p><p><p>At this point D opens B through intent, then the structure of the stack becomes: a B. C and D are destroyed by the stack eject, which means that instances above B are destroyed. Instead of creating a new B instance, use the B in the stack, and call the Onnewintent () method of the original B. such as (5), (6)</p></p><p><p></p></p><p><p>Figure (5)</p></p><p><p></p></p><p><p>Figure (6)</p></p><p><p>Experience: This mode is more common, in the activity stack, because the application needs, often need to open more than one of the same or different activity, then the activity stack will grow larger, thus consuming more and more memory, if the activity configured this property is invincible , what would it do? if you open a B C D E ... wait, If this time you need to open a, but want to destroy B C D E ... , this property satisfies the requirement, and you will find that your memory usage is down because b C D E ... have been Destroyed.</p></p><p><p>Experience: of course, is not to say that set up singletask on everything, said before, each usage has its own truth, Singletask want to do is to show that the current stack of existing activity is not recreated, but re-use, If the activity that needs to be opened already exists in the stack, then the other activity above the activity in the stack is destroyed, exposing the existing activity and executing its onnewintent method.</p></p><p><p>of course, if the current stack does not exist, create a new one that is placed on top of the STACK.</p></p><p><p>A word of memory: as long as a exists in the stack, then the whole of a is destroyed (does not contain a), while displaying and reusing a, the execution of the onnewintent method. otherwise, Create a new A that is placed on top of the STACK.</p></p><p><p><strong>D. singleinstance</strong>:</p></p><p><p>In SingleInstance mode, a new task stack is Created.</p></p><p><p>For example, the structure of the current stack is: A B C D.</p></p><p><p>Open e (mode singleinstance) in D via intent, a new task Stack 2 is created, the structure in the stack task 1 is still a B C D, and the structure in stack 2 is e, and E is displayed on the Screen.</p></p><p><p>This type of pattern is typically used when another app is Opened. For example: call, Use high-level, consumption of resources applications. Open in the app, Sina Weibo and other Clients. such as (7)</p></p><p><p></p></p><p><p>Figure (7)</p></p><p><p><strong>5. Activity</strong> <strong>Stack and task contact</strong></p></p><p><p>Task is simply a collection of activity components clustered together in a stack of patterns, similar to a container filled with activity, the first activity to be placed at the bottom of the container, the last to be added to the top of the container, The activity from the task is removed from the top, and finally the activity is added at the very beginning, which is the LIFO (last on first Out) mode, and the order of the activity in the task can be controlled, Intent flag can be used to set up new activity when the activity jumps (this involves the use of intent flag).</p></p><p><p></p></p><p><p><strong>6. Intent Flags</strong></p></p><p><p>Flags: represents the intent, commonly used in the activity scene, it is closely related to the activation mode of the activity, simply speaking, the effective combination of flag (usually with "|" Combination) determines how the activity is Opened.</p></p><p><p>The flags properties related to this topic are listed Below:</p></p><p><p>(1)--intent.flag_activity_new_task (default)</p></p><p><p>The default jump type, which re-creates a new Activity.<br><br></p></p><p><p>(2)--flag_activity_single_top</p></p><p><p>This flag is equivalent to the singletop in the boot mode, please refer to the Singletop instructions.</p></p><p><p>(3)--flag_activity_clear_top</p></p><p><p>This flag-initiated activity will pop up the stack space on the activity that is being activated (including itself). For example: the structure in the original stack is a B c D, jump from D to b, release order D C b, and then re-create B on the top of the stack, the structure of the stack is changed to a B. (this method can be used to close multiple Activity)</p></p><p><p>Experience: you need to destroy multiple ACTIVITY in a stack before a, but do not want to destroy a, you need to use Flag_activity_clear_top and flag_activity_single_top combination<br><br></p></p><p><p>(4)--flag_activity_clear_task</p></p><p><p>This flag is used with the preceding conditions: API level 11 or higher and needs to be used with Flag_activity_new_task.</p></p><p><p>This identity, which frees all activity in the current stack, and then creates a new activity object at the top of the Stack.</p></p><p><p>For example, the original A b C D in the stack, you need to jump from D to b, then release D C b A, then create B, placed on top of the STACK.</p></p><p><p><strong>7. Activity-</strong> <strong>related properties taskaffinity</strong></p></p><p><p>The Android:taskaffinity property in Activity describes:</p></p><p><p>Activity is a affinity owned by a Task. Activity with the same affinity is theoretically the same task (the same "application" in the User's perspective). The affinity of a task is determined by its root Activity.<br>Affinity determines that two things--activity the task of the Re-host (reference allowtaskreparenting Attribute) and the ACTIVITY host initiated with the Flag_activity_new_task Flag.<br>By default, all activity in an application has the same Affinity. Pinch can set this feature to reorganize them, and can even put the activity defined in different applications into the same task. To make it clear that the activity does not host a particular task, set the string with an empty Attribute.<br>If this feature is not set, the activity will inherit from the Application's settings (refer to the taskaffinity feature of the <application> element). The default affinity name of the application is the package name set in the <manifest> Element.</p></p><p><p>Android:taskaffinity This property of the activity will only take effect if the activity is initiated by the intent of the flag bit Flag_activity_new_task. The system will switch a task with the same task affinity to the foreground and then start the activity, otherwise the activity is still running in the task that started it.</p></p><p><p></p></p><p><p>Transferred from: http://my.oschina.net/u/2249934/blog/325467</p></p><p><p>An analysis of "android in depth" activity startup mode</p></p></span>
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