Life cycle of Android--activity (i.)

Source: Internet
Author: User
<span id="Label3"></p>1. Activity creation Process 1, 1 Create an activity <ul> <ul> <li>Create a class to inherit from activity</li> <li>Override the OnCreate () method to set the layout</li> <li>Register the activity in the Androimanifest.xml file</li> </ul> </ul>1, 2 Activity7 An important method <ul> <ul> <li>Oncreate– called when first created</li> <li>onstart– at startup</li> <li>When the onresume– gets focus</li> <li>onpause– transfer to other activity or hibernation, etc.</li> <li>onstop– Current activity is not visible</li> <li>onrestart– when restarting</li> <li>ondestory– when destroyed</li> </ul> </ul>2. Single Activity life cycle<p><p>To confirm the conclusion correctly, we create a new activity named fisrt, and rewrite the above 7 methods. and prints the method that is currently called in each method. The code is as Follows:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); System.out.println (<span class="hljs-string"><span class="hljs-string">"first onCreate"</span></span>); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnStart</span></span>() {System.out.println (<span class="hljs-string"><span class="hljs-string">"first onStart"</span></span>);<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnStart (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Onresume</span></span>() {System.out.println (<span class="hljs-string"><span class="hljs-string">"first onresume"</span></span>);<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Onresume (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnPause</span></span>() {System.out.println (<span class="hljs-string"><span class="hljs-string">"first onPause"</span></span>);<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnPause (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnStop</span></span>() {System.out.println (<span class="hljs-string"><span class="hljs-string">"first onStop"</span></span>);<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnStop (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Onrestart</span></span>() {System.out.println (<span class="hljs-string"><span class="hljs-string">"fisrt onrestart!"</span></span>);<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Onrestart (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnDestroy</span></span>() {System.out.println (<span class="hljs-string"><span class="hljs-string">"first ondestory!"</span></span>);<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnDestroy (); }</code></pre><p><p>Now we start the activity for the first Time. discovery Output:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs mathematica"><span class="hljs-keyword">First</span> onCreate<span class="hljs-keyword">First</span> onStart<span class="hljs-keyword">First</span> onResume</code></pre></pre><p><p>When we click Home to return to the main interface, the output is:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs mathematica"><span class="hljs-keyword">First</span> onPause<span class="hljs-keyword">First</span> onStop</code></pre></pre><p><p>In the click program, go back to the program, output for</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs mathematica"><span class="hljs-keyword">First</span> onRestart<span class="hljs-keyword">First</span> onStart<span class="hljs-keyword">First</span> onResume</code></pre></pre><p><p>When you click back, the output is</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs nginx"><span class="hljs-title">First</span><span class="hljs-built_in">on</span><span class="hljs-built_in">on</span><span class="hljs-built_in">on</span>Destory</code></pre></pre><p><p>The above output can clearly see the start-up process, not much to Describe.</p></p>3. Multiple activity life cycle<p><p>The two activity is created here, respectively, first and second,2, in fact, are the Same.<br>We added a button to the first activity, and when you click the button, you use intent to jump to second,<br>Jump Code:</p></p><pre class="prettyprint"><code class="language-java hljs "> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); System.out.println (<span class="hljs-string"><span class="hljs-string">"first onCreate"</span></span>); Button btn = (button) Findviewbyid (r.id.btn); Btn.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View Arg0) {Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (); Intent.setclass (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, secondactivity.class); StartActivity (intent); } }); }</code></pre><p><p>How to add a button control is not Elaborated.<br>Below we launch the application, which goes into first Activity. According to 2, the output order should be:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs livecodeserver"><span class="hljs-keyword">first</span> onCreate<span class="hljs-keyword">first</span> onStart<span class="hljs-keyword">first</span> onResume</code></pre></pre><p><p>Then we click on the button to jump to second activity and the output is:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs livecodeserver"><span class="hljs-keyword">first</span> onPause<span class="hljs-keyword">second</span> onCreate<span class="hljs-keyword">second</span> onStart<span class="hljs-keyword">second</span> onResume<span class="hljs-keyword">first</span> onStop</code></pre></pre><p><p>At this time the activities for the second activity, so at this point home operation, and the same as Above.<br>If we click Back to return to, then the output is</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs livecodeserver"><span class="hljs-keyword">second</span> onPause<span class="hljs-keyword">first</span> onRestart<span class="hljs-keyword">first</span> onStart<span class="hljs-keyword">first</span> onResume<span class="hljs-keyword">second</span> onStop<span class="hljs-keyword">second</span> onDestory</code></pre></pre><p><p>At this time second activity has been destroy off, if re-enter is not the same as above, but re-create a new activity, jump to second activity again, the output is:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs livecodeserver"><span class="hljs-keyword">first</span> onPause<span class="hljs-keyword">second</span> onCreate<span class="hljs-keyword">second</span> onStart<span class="hljs-keyword">second</span> onResume<span class="hljs-keyword">first</span> onStop</code></pre></pre>4. Summary<p><p>The above program output has put the order of the call, explained very clearly. We recommend that you overload those 7 functions, and then start it yourself, and experiment to increase the understanding.</p></p> <p><p>Life cycle of Android--activity (i.)</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.