The life cycle of Android activity

Source: Internet
Author: User

Activity is the four components of Android (the other three Contentprovider,service, broadcast (Broadcastreceiver)). Represented in wirelessly is a page of the program. A screen for user weight, responsible for interacting with the user.
The activity class must be inherited to create the activity. Of the two most important methods in a custom activity:
1. OnCreate (Bundle) Method: This method initializes the activity method. This method must be implemented because the system automatically calls this method when the activity is created, and when implementing this method, the necessary components should be initialized and, more importantly, the layout of the user interface must be set for the activity using the Setcontentview () method.
2. OnPause () Method: When the user is ready to leave the activity, save the users ' data, activity status and so on.
In order to be able to use context.startactivity (), all activity classes must define the relevant activity tag in the Androidmanifest.xml master configuration file.
Activity has three essential differences in state:
1, resumed: When the activity in the foreground to get the user's focus (also known as running state)
2. Paused: Another activity is shown in the front end, getting focus, but the activity is still visible. Another activity is visible, partly transparent, not completely covering the entire phone screen, consuming less memory resources, or ending the process when the phone is in dire need of memory resources.
3. Stopped: An activity is completely covered by another activity. But it still maintains all the state and member information, but it is no longer visible, so its window is hidden and when the system memory needs to be used elsewhere, stopped's activity will be killed.

Activity life cycle diagram:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5B/E6/wKioL1UWWwnwiZK9AAGs0QcorzM900.jpg "title=" 2015-03-28_144630.png "alt=" Wkiol1uwwwnwizk9aags0qcorzm900.jpg "/>

A total of seven declaration cycles:

· OnCreate: Called when the activity is first created

· OnStart: Called when the activity is visible to the user

· Onresume: Called when activity starts and user interaction

· OnPause: Called when the current activity is paused and the previous activity is resumed

· OnStop: Called when the activity is no longer visible to the user

· Ondestory: Called before an activity is destroyed by the system (either manually or by the system to save memory)

· Onrestart: Called when the activity has stopped and wants to start again

start activity Main method, but through Activity method. In android The program starts, and when we can see it, it has been oncreate---> OnStart--->onresume three processes, when you want to know more about Span style= "Font-size:12px;font-family:verdana;" >activity life cycle, you can override activity inside

protected void onCreate (Bundle savedinstancestate);

protected void OnStart ();

protected void Onrestart ();

protected void Onresume ();

protected void onPause ();

protected void onStop ();

protected void OnDestroy ();

Seven ways to do this. Add output statements, simulate various operations, and view the output in Logcat .

The mainactivity is as follows:

Package com.zzh.activity;


Import android.app.Activity;

Import Android.os.Bundle;

Import Android.util.Log;


public class Mainactivity extends Activity

{

@Override

protected void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

LOG.I ("tag", "--------------> OnCreate <-----------");

}


@Override

protected void OnStart ()

{

Super.onstart ();

LOG.I ("tag", "--------------> OnStart <-----------");

}


@Override

protected void Onrestart ()

{

Super.onrestart ();

LOG.I ("tag", "--------------> Onrestart <-----------");

}


@Override

protected void Onresume ()

{

Super.onresume ();

LOG.I ("tag", "--------------> Onresume <-----------");

}


@Override

protected void OnPause ()

{

Super.onpause ();

LOG.I ("tag", "--------------> OnPause <-----------");

}


@Override

protected void OnStop ()

{

Super.onstop ();

LOG.I ("tag", "--------------> OnStop <-----------");

}


@Override

protected void OnDestroy ()

{

Super.ondestroy ();

LOG.I ("tag", "--------------> OnDestroy <-----------");

}

}


When the program launches into the page:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/E6/wKioL1UWYv3wwm-gAABA6eHz8q4008.jpg "title=" 1.png " alt= "Wkiol1uwyv3wwm-gaaba6ehz8q4008.jpg"/>

Log information that appears in Logcat:---->resumed

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/EC/wKiom1UWYmThLBhRAADjXUJEu1U663.jpg "title=" 1.png " alt= "Wkiom1uwymthlbhraadjxujeu1u663.jpg"/> When exiting the program: Press the log message that appears in the return key esc,logcat: Stopped

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5B/E6/wKioL1UWZPDT__vxAADakCJ_ngI022.jpg "title=" 1.png " alt= "Wkiol1uwzpdt__vxaadakcj_ngi022.jpg"/> If there is a call and then hang up, the log message appears in the Logcat at this time:--paused--->resumed

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/EC/wKiom1UWZ_zRc8qgAAFkkD3jGbA374.jpg "title=" 1.png " alt= "Wkiom1uwz_zrc8qgaafkkd3jgba374.jpg"/>

Three states completed.

This article is from the "JDK7 Environment Building" blog, please make sure to keep this source http://zzhhz.blog.51cto.com/7107920/1625988

The life cycle of Android activity

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.