The life cycle of Android actitity

Source: Internet
Author: User

To create a new project, mainactivity code is as follows:

 Packagecom.wuyudong.lifecycle;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); System.out.println ("OnCreate"); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); System.out.println ("OnDestroy"); } @Overrideprotected voidOnPause () {Super. OnPause (); System.out.println ("OnPause"); } @Overrideprotected voidOnresume () {Super. Onresume (); System.out.println ("Onresume"); } @Overrideprotected voidOnStart () {Super. OnStart (); System.out.println ("OnStart"); } @Overrideprotected voidOnStop () {Super. OnStop (); System.out.println ("OnStop"); }    }

Run the project and view the print information in Logcat:

After you close the app, print additional destruction information

Life cycle of activity

What is the life cycle?
The methods that must be executed from the time the object is created to the stage of the destruction, which are the callbacks for the life cycle

Activity life cycle:
New-gc

OnCreate () method to invoke when activity is created
Initialization of the UI interface Setcontentview ()

OnStart () Activity interface user visible
Update UI action, play video

OnDestroy () method to invoke when activity is destroyed
Interface exit before the wrap-up operation, SMS transmitter, the data before exiting the save.

OnStop () Activity interface user is no longer visible
Screen is not visible, pause video playback

Onresume ()
The interface gets the focus, and the button can click the event

OnPause ()
When the interface loses focus, the button cannot click the event

Onrestart ()
The activity is minimized and not destroyed, and if you open the activity again next time,
Re-user interface visible

Activity has three states:
1. When it is in the foreground of the screen (at the top of the current task stack), it is active or running. It is the activity that responds to user actions.
2. When it loses focus but remains visible to the user (as on the right), it is in a paused state. There is another activity on top of it. The activity may be transparent or not fully covered, so the suspended activity is still visible to the user. The paused activity is still alive (it retains all state and member information and remains connected to the window manager), but the activity can still be killed when the system is in very low memory.
3. It is in a stopped state when it is completely overwritten by another activity. It still retains all the status and member information. However, the user is invisible, so its window will be hidden, and if memory is needed elsewhere, the system will often kill the activity.


When activity transitions from one state to another, the following protection methods are called to notify this change:
void OnCreate (Bundle savedinstancestate)
void OnStart ()
void Onrestart ()
void Onresume ()
void OnPause ()
void OnStop ()
void OnDestroy ()

These seven methods define the full life cycle of the activity. Implementing these methods can help us monitor three of these nested lifecycle loops:

1. The full life cycle of the activity starts with the first call to OnCreate () until OnDestroy () is called. Activity sets all the "global" states in OnCreate () to complete initialization, and frees all system resources in OnDestroy (). For example, if an activity has a thread running in the background downloading data from the network, it creates the thread in OnCreate () and destroys the thread in OnDestroy ().

2. The activity's visual life cycle starts from OnStart () call until the corresponding OnStop () call ends. During this time, the user can see the activity on the screen, although it may not be in the foreground or interact with the user. Between the two methods, we can keep the resources needed to show the activity to the user. For example, when a user no longer sees what we are displaying, we can register a broadcastreceiver in OnStart () to monitor changes that affect the UI, and in OnStop (). The OnStart () and OnStop () methods can be called multiple times as the application is visible to the user.

3, the activity of the foreground life cycle from Onresume () call up, to the corresponding OnPause () call so far. During this time, the activity is at the top of the foreground and interacting with the user. Activity often transitions between pauses and recoveries-for example, when a device goes into hibernation or a new activity starts, the OnPause () method is called. The Onresume () method is called when the activity obtains the result or receives a new intent. For an example of the foreground lifecycle cycle, see the Remarks section below PPT.

Next, Practice:

New project, New activity page: secondactivity

The layout is as follows:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " >    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Open a second interface" /></Relativelayout>

Add a Click event

     Public void Click (View view) {        new Intent (This, secondactivity.  Class);        StartActivity (intent);    }

Run the project and observe the Logcat printing information

When the first interface appears, print the following sequence: Oncreate->onstart->onresume

When the button is clicked, Print: Onpause->onstop

Next Add a third button

    < Button         Android:layout_width = "Wrap_content"         android:layout_height= "Wrap_content"        android:layout_centerhorizontal  = "true"        android:layout_alignparentbottom= "true"        Android:onclick  = "Click2"        android:text= "Open a third interface"/>

Add the activity that corresponds to the jump and add theme to the manifest file

        <!---         <activity             android:theme= "@ Android:style/theme.dialog "            android:name=". Thirdactivity "            android:label=" @string/activity03 "             ></ Activity >

Watch Logcat Print information when you click the button

Get more knowledge of C language and algorithms, follow the public number: "Csuanfa"

The life cycle of Android actitity

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.