The life cycle of the activity of the Android series tutorials

Source: Internet
Author: User
Tags export class

through the previous section, "Android series tutorial directory structure for Android projects "We already know what activity is, so why do we have to overwrite the activity's OnCreate method when we create an activity's export class, why it's in OnPause () method to save some changes in the current activity, to understand that it is necessary to understand the activity of the life cycle, that is, an activity only to go through the end of the state, following an example to understand the activity of the declaration cycle.

One: The life cycle approach of activity

Android provides a lot of activity life cycle methods, such as OnCreate, OnPause, Onresume, etc., which we commonly use. This paper mainly introduces coarse-grained periodic methods, such as Onpostcreate, Onpostresume, etc.
These fine-grained cycle methods can refer to the Android API documentation and use these fine-grained methods when you need finer levels of control. The coarse-grained cycle method has the following:
OnCreate (), OnStart (), Onresume (), OnPause (), OnStop (), OnDestroy (), can be seen from the name of when these methods are executed.

Second: Test the execution order of the activity's life cycle methods

In order to better understand the order of execution of these cycles, we create a new HelloWorld project that covers these methods in the activity and prints out the log to see the order of execution.

    1. 1. New HelloWorld Project

2. Modification The Activity_main.xml code is as follows:

<?xml version= "1.0"  encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Fill_parent "     android:layout_height= "fill_parent"     android:orientation= "vertical"   >    <textview        android:layout_width= " Fill_parent "        android:layout_height=" Wrap_content "         android:text= "First Activity"  />    <button         android:id= "@+id/second"          android:layout_width= "Wrap_content"         android:layout_ height= "Wrap_content"         android:text= "open a second activity"  /> </LinearLayout>

The main point here is to add a text display and a button for displaying information and actions.

3, the new layout file Second.xml, the content is as follows:

<?xml version= "1.0"  encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Fill_parent "     android:layout_height= "fill_parent"     android:orientation= "vertical"   >    <textview        android:layout_width= " Fill_parent "        android:layout_height=" Wrap_content "         android:text= "A second activity"  />    <button         android:id= "@+id/exit"          android:layout_width= "Wrap_content"         android:layout_ height= "Wrap_content"         android:text= "Exit"  /></ Linearlayout>

This is mainly for adding a text display and an Exit button to exit the current activity.

4, A new activity, the name of Secondactivity, to Androidmanifest.xml also declare, otherwise error:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/81/wKioL1Y7AjXysz9CAAI9RBv3NB8060.jpg "title=" 1.png "alt=" wkiol1y7ajxysz9caai9rbv3nb8060.jpg "/>

secondactivity class reads as follows:  

import android.app.activity;import android.os.bundle;import android.util.log;import  android.view.view;import android.view.view.onclicklistener;import android.widget.button;public  class secondactivity extends activity{private final static string tag  =  "Secondactivity";p rotected void oncreate (bundle savedinstancestate) {super.onCreate ( Savedinstancestate); Setcontentview (r.layout.second);//Exit button button btnexit =  (button)   Findviewbyid (r.id.exit);//For the Exit button set Click event Btnexit.setonclicklistener (New onclicklistener ()  {@ Overridepublic void onclick (VIEW&NBSP;ARG0)  {//Close the current activity, that is, exit finish ();});} Overwrite  android.app.activity.onstart method Protected void onstart () {Super.onstart (); LOG.V (tag,  "OnStart");} Overwrite  android.app.activity.onresumeprotected void onresume () {super.onresume (); LOG.V (tag,  "Onresume");} Protected void onpause () {Super.onpause (); LOG.V (tag,  "OnPause");} Protected void onstop () {super.onstop (); LOG.V (tag,  "OnStop");} Protected void ondestroy () {Super.ondestroy (); LOG.V (TAG, "OnDestroy");}}

I've added log information to each of the cycle methods to keep track of how the activity is running.

5, Modify the HelloWorld class, the content is as follows:

import android.os.bundle;import android.app.activity;import android.content.intent;import  android.util.log;import android.view.view;import android.view.view.onclicklistener;import  Android.widget.button;public class helloworld extends activity {private final  static String TAG =  "HelloWorld"; @Overrideprotected  void oncreate (Bundle  savedinstancestate)  {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);// Open the Second Activity button button btnsecond =  (button)  findviewbyid (R.id.second); Btnsecond.setonclicklistener (New onclicklistener ()  {@Overridepublic  void onclick (View &NBSP;ARG0)  {//jump to Secondactivitystartactivity (New intent (Helloworld.this,secondactivity.class)); Finish ();//close Current Activity}}); /* *  overrides the method of overwriting  android.app.activity  *  */protected void onstart () { Super.onstart (); LOG.V (TAG,&NBSP; " OnStart ");} Protected void onresume () {super.onresume (); LOG.V (tag,  "Onresume");} Protected void onpause () {super.onpause (); LOG.V (tag,  "OnPause");} Protected void onstop () {super.onstop (); LOG.V (tag,  "OnStop");} Protected void ondestroy () {Super.ondestroy (); LOG.V (TAG, "OnDestroy");}}

6, run the program, analyze the results, found that when the program started, the log information is:  


  1. 650) this.width=650; "Src=" http://dl.iteye.com/upload/attachment/349655/baddee2d-f46b-347d-b1f4-10b902def717.png "Style=" border:0px; "/>
    This shows that when an activity is opened, its cycle method execution order is: OnCreate ()->onstart ()->onresume (), now click on the "Open second Activity" button to see the output of the log such as:
    650) this.width=650; "Src=" http://dl.iteye.com/upload/attachment/349660/2fcfc919-cae3-36a5-9536-0bae63cc0636.png "Style=" border:0px; "/>
    When the app starts secondactivity from the HelloWorld activity, Android executes the HelloWorld OnPause method First, then Secondactivity OnCreate () in turn- >onstart ()->onresume () method
    Once the secondactivity renders to the screen, once again executes HelloWorld's OnStop ()->ondestroy (), removing the HelloWorld from the activity stack for destruction. What is worth mentioning here is HelloWorld in the finish method, because executed him so
    HelloWorld removes the destruction from the activity stack, so that when you press the "Back" key to return, you will not be back to the HelloWorld activity interface, but instead go back to the Android app list directly.

Three: Analysis of the results

So we're going to have to prepare the activity's needs in the OnCreate method, that is, to initialize it, to make some adjustments in the activity in the Onresume, to do some cleanup and preservation work in OnPause (save the persistent state), because this is the last
Opportunity, because Android does not end the process of hosting the activity class until the OnPause is complete, and the process may end later. Summarize the effects of these periodic methods:

    1.  

      1. OnCreate (): Create Activity call, For activity initialization, there is also a bundle type parameter that can access the previously stored state.

      2. OnStart (): Called when activity is visible to the user on the screen

      3. Onresume (): Called when activity starts interacting with the user, The activity is at the top of the activity stack.

      4. OnPause (): Called when activity is paused, that is, when you say byebye to the activity you can see, you can do some cleanup and save work

      5. OnStop (): Call

      6. OnDestroy () When activity is stopped or activity becomes invisible: activity is removed from memory, Typically occurs when the finish method is executed or when Android reclaims memory


Well, let's take a look at the status diagram of the activity provided by the API, and see that he believes that you will know more about the life cycle of the activity, such as:
650) this.width=650; "Src=" http://dl.iteye.com/upload/attachment/349672/df78b0d1-8998-38ce-a5b7-95632d1192ac.png "Style=" border:0px; "/>

Four: summary

This section focuses on the activity declaration cycle through an example, and describes the common life cycle methods, when they should be used. Here Android's very basic stuff has been said!


This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1710029

The life cycle of the activity of the Android series tutorials

Related Article

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.