Android activity life cycle and startup mode

Source: Internet
Author: User
<span id="Label3"></p><p><p>The full life cycle of the activity is as Follows:</p></p><p><p></p></p><p><p>There are four modes of activity loading:</p></p><p><p>Standard: normal mode, The default load mode, each time the target acitivity is launched through this mode, a new instance is created and the activity is added to the current Stack.</p></p><p><p>Singletop: similar to standard mode, when activity is at the top of a task, the system does not recreate the example of the target activity, but instead directly re-uses the existing activity instance.</p></p><p><p>Singletask:</p></p><p><p>If the activity to start does not exist, the system creates an activity instance and adds it to the top of the stack</p></p><p><p>If the activity to be started exists, it is already at the top of the stack, and the singletop behaves the same</p></p><p><p>If the activity to be started exists, but not at the top of the stack, the system causes all activity above the activity to Stack.</p></p><p><p>Singleinstance:</p></p><p><p>If the activity to start does not exist, a new task is created and the activity instance is created, adding it to the top of the new task</p></p><p><p>If the activity to be started exists, regardless of which application it is in, the system will transfer the Activity's task to the Foreground.</p></p><p><p></p></p><p><p>The following verifies in turn how the Activity's lifecycle is performed in four load Modes. Suppose There's activity a,b,c</p></p><p><p>1. Standard mode start a->b->c</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Start a</span></span>D/activitya (19864<span style="color: #000000;"><span style="color: #000000;">): oncreated</span></span>/activitya (19864<span style="color: #000000;"><span style="color: #000000;">): ONSTARTD</span></span>/activitya (19864<span style="color: #000000;"><span style="color: #000000;">): Onresume</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Start B</span></span>D/activitya (19864<span style="color: #000000;"><span style="color: #000000;">): onpaused</span></span>/activityb (19864<span style="color: #000000;"><span style="color: #000000;">): oncreated</span></span>/activityb (19864<span style="color: #000000;"><span style="color: #000000;">): ONSTARTD</span></span>/activityb (19864<span style="color: #000000;"><span style="color: #000000;">): onresumed</span></span>/activitya (19864<span style="color: #000000;"><span style="color: #000000;">): onStop</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">start C</span></span>D/activityb (19864<span style="color: #000000;"><span style="color: #000000;">): onpaused</span></span>/ACTIVITYC (19864<span style="color: #000000;"><span style="color: #000000;">): oncreated</span></span>/ACTIVITYC (19864<span style="color: #000000;"><span style="color: #000000;">): ONSTARTD</span></span>/ACTIVITYC (19864<span style="color: #000000;"><span style="color: #000000;">): onresumed</span></span>/activityb (19864): OnStop</pre><p><p>Then press back to Build:</p></p><pre><pre>D/ACTIVITYC (19864<span style="color: #000000;">): onpaused</span>/activityb (19864<span style="color: #000000;">): onrestartd</span>/activityb (19864<span style="color: #000000;">): onstartd</span>/ Activityb (19864<span style="color: #000000;">): onresumed</span>/activityc (19864<span style="color: #000000;">): onstopd</span>/activityc (19864): ondestory</pre></pre><p><p>So if Ondestory is not called, OnCreate is not called, but Onrestart is Called.</p></p><p><p></p></p><p><p>2, A->b->a</p></p><pre><pre>D/activitya (19864<span style="color: #000000;">): oncreated</span>/activitya (19864<span style="color: #000000;">): onstartd</span>/activitya (19864<span style="color: #000000;">): onresumed</span>/ Activitya (19864<span style="color: #000000;">): onpaused</span>/activityb (19864<span style="color: #000000;">): oncreated</span>/activityb (19864<span style="color: #000000;">): onstartd</span>/ Activityb (19864<span style="color: #000000;">): onresumed</span>/activitya (19864<span style="color: #000000;">): onstopd</span>/activityb (19864<span style="color: #000000;">): onpaused</span>/ Activitya (19864<span style="color: #000000;">): oncreated</span>/activitya (19864<span style="color: #000000;">): onstartd</span>/activitya (19864<span style="color: #000000;">): onresumed</span> /activityb (19864): OnStop</pre></pre><p><p>Modify the startup mode of A to Singletop</p></p><p><p>->a->a</p></p><pre><pre>D/activitya (27075<span style="color: #000000;">): oncreated</span>/activitya (27075<span style="color: #000000;">): onstartd</span>/activitya (27075<span style="color: #000000;">): onresumed</span>/ Activitya (27075<span style="color: #000000;">): onpaused</span>/activitya (27075<span style="color: #000000;">): onnewintentd</span>/activitya (27075): onresume</pre></pre><p><p>->a->b->a</p></p><pre><pre>D/activitya (27075<span style="color: #000000;">): oncreated</span>/activitya (27075<span style="color: #000000;">): onstartd</span>/activitya (27075<span style="color: #000000;">): onresumed</span>/ Activitya (27075<span style="color: #000000;">): onpaused</span>/activityb (27075<span style="color: #000000;">): oncreated</span>/activityb (27075<span style="color: #000000;">): onstartd</span>/ Activityb (27075<span style="color: #000000;">): onresumed</span>/activitya (27075<span style="color: #000000;">): onstopd</span>/activityb (27075<span style="color: #000000;">): onpaused</span>/ Activitya (27075<span style="color: #000000;">): oncreated</span>/activitya (27075<span style="color: #000000;">): onstartd</span>/activitya (27075<span style="color: #000000;">): onresumed</span> /activityb (27075): OnStop</pre></pre><p><p>When a is not the top of the stack, start a, re-create a, and observe the above output, two activity switching, the activity first onpause, then started activity, in turn oncreate, onStart, The previous activity will be onstop after the Onresume is Shown.</p></p><p><p>Modify a to start with singletask,</p></p><p><p>->a->a</p></p><pre><pre>D/activitya (2744<span style="color: #000000;">): oncreated</span>/activitya (2744<span style="color: #000000;">): onstartd</span>/activitya (2744<span style="color: #000000;">): onresumed</span>/ Activitya (2744<span style="color: #000000;">): onpaused</span>/activitya (2744<span style="color: #000000;">): onnewintentd</span>/activitya (2744): onresume</pre></pre><p><p>When a does not exist, create a, when a exists and at the top of the stack, first onpause, then onnewintent, then Onresume.</p></p><p><p>->a->b->a</p></p><pre><pre> D/activitya (2744<span style="color: #000000;" ): oncreated < span>/activitya (2744 <span style="color: #000000;" ): onstartd < span>/activitya (2744<span style="color: #000000;" ): onresumed < span> /activitya (2744<span style="color: #000000;" ): onpaused < span>/activityb (2744<span style="color: #000000;">): oncreated </span>/activityb (2744<span style="color: #000000;" ): onstartd < span>/activityb (2744<span style="color : #000000; " ): onresumed < span>/activitya (2744<span style="color: #000000;" ): onstopd < span>/activityb (2744<span style=" Color: #000000; ">): onpaused </span>/activitya (2744<span style="color: #000000;" ): onnewintentd < span>/activitya (2744<span style="color: #000000;" ): onrestartd < span>/activitya (2744<span style="color: #000000;" ): onstartd < span>/activitya (2744<span style=" Color: #000000; ">): onresumed </span>/activityb (2744<span style="color: #000000;" ): onstopd < span>/activityb (2744): ondestory </span></span></span></span></span></span></span></span></span></span></span></pre></pre><p><p>You can see the second start a after a call to onnewintent,onrestart,onstart,onresume, the key is to tune the B onstop, ondestory, in the previous two modes just another b, called onstop, so inferred that singletask, the previous a onnewintent re-entered the onresume, then the B was removed from the STACK.</p></p><p><p>Modify a to start with singleinstance</p></p><p><p>->a->a</p></p><pre><pre>D/activitya (10578<span style="color: #000000;">): oncreated</span>/activitya (10578<span style="color: #000000;">): onstartd</span>/activitya (10578<span style="color: #000000;">): onresumed</span>/ Activitya (10578<span style="color: #000000;">): onpaused</span>/activitya (10578<span style="color: #000000;">): onnewintentd</span>/activitya (10578): onresume</pre></pre><p><p>Consistent with Singletask performance</p></p><p><p>->a->b->a</p></p><pre><pre>D/activitya (10578<span style="color: #000000;">): oncreated</span>/activitya (10578<span style="color: #000000;">): onstartd</span>/activitya (10578<span style="color: #000000;">): onresumed</span>/ Activitya (10578<span style="color: #000000;">): onpaused</span>/activityb (10578<span style="color: #000000;">): oncreated</span>/activityb (10578<span style="color: #000000;">): onstartd</span>/ Activityb (10578<span style="color: #000000;">): onresumed</span>/activitya (10578<span style="color: #000000;">): onstopd</span>/activityb (10578<span style="color: #000000;">): onpaused</span>/ Activitya (10578<span style="color: #000000;">): onnewintentd</span>/activitya (10578<span style="color: #000000;">): onrestartd</span>/activitya (10578<span style="color: #000000;">): ONSTARTD </span>/activitya (10578<span style="color: #000000;">): onresumed</span>/activityb (10578): onStop</pre></pre><p><p>In SingleInstance mode, The original A is reused, the B is only onstop, and no stack destruction occurs.</p></p><p><p>Summary above:</p></p><p><p>When reusing an existing activity, it is usually started from its onnewintent or onrestart,</p></p><p><p>If an activity is to be out of the stack, it must be transferred to Ondestory.</p></p><p><p>In the two activity switching process, is the current activity first onpause, and then let the new activity create or restart, know onresume, the previous activity will go onstop and ondestory</p></p><p><p></p></p><p><p>Another problem: starting B in the A life cycle function (a, a, a, standared), respectively,</p></p><pre><pre>D/activitya (17860<span style="color: #000000;">): oncreated</span>/activitya (17860<span style="color: #000000;">): onstartd</span>/activitya (17860<span style="color: #000000;">): onresumed</span>/ Activitya (17860<span style="color: #000000;">): onpaused</span>/activityb (17860<span style="color: #000000;">): oncreated</span>/activityb (17860<span style="color: #000000;">): onstartd</span>/ Activityb (17860<span style="color: #000000;">): onresumed</span>/activitya (17860): onStop</pre></pre><p><p>The life cycle callback function is complete and will be transferred sequentially, but the problem is that when you press the back key, the following appears:</p></p><pre><pre>D/activityb (19588<span style="color: #000000;">): onpaused</span>/activitya (19588<span style="color: #000000;">): onrestartd</span>/activitya (19588<span style="color: #000000;">): onstartd</span>/ Activitya (19588<span style="color: #000000;">): onresumed</span>/activitya (19588<span style="color: #000000;">): onpaused</span>/activityb (19588<span style="color: #000000;">): oncreated</span>/ Activityb (19588<span style="color: #000000;">): onstartd</span>/activityb (19588<span style="color: #000000;">): onresumed</span>/activityb (19588<span style="color: #000000;">): onstopd</span>/ Activityb (19588<span style="color: #000000;">): ondestoryd</span>/activitya (19588): onStop</pre></pre><p><p>Need to return a, but a in the life cycle of start b, then the behavior is related to the startup mode, always it is normal to switch the correct execution order.</p></p><p><p></p></p><p><p>This knowledge point is too round, in fact original aim, are four kinds of startup mode life cycle execution sequence Combination.</p></p><p><p>Android activity life cycle and startup mode</p></p></span>

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.