The life cycle of Android activity

Source: Internet
Author: User

1. Activity Brief Introduction:

Activity can be easily understood as an Android phone app for every interface.

It has a corresponding Java class file to implement the Activity Class (equivalent to the phone interface control related logic files, similar to the Flex page script file, or the common page form JS),

There is also a response to the layout of the XML file, which pre-sets the responsive layout control and its size, color, and other properties.

Each activity needs to be registered in the Androidmanifest.xml file, similar to Javaweb in Servlet,listener need to register in Web. Xml.

2, Activity life cycle diagram, such as the following:


A few simple stages are explained:

1), for example, open a login activity named Loginactivity. The loginactivity will run OnCreate first. Onstart,onresume method. The initialization of the login interface is complete.

2), if the user click Back to navigate to the main menu, then run loginactivity onpause,onstop (users do not see the login screen run), Ondestory method

Onrestart use case descriptions below.

3, Case Description:

Brief introduction: A total of two interfaces. A main interface, called testlifecycleactivity (required), a click on the main interface button to go to the sub-interface, named TestLifeCycleActivity2, code and layout files such as the following:

Testlifecycleactivity.java

Package Com.example.helloworld;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Testlifecycleactivity extends Activity {private int i = 1;private Button test_life_cycle_btn1 = null; @Overrideprotected vo ID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.test_life_ cycle); SYSTEM.ERR.PRINTLN (i + "," + "testlifecycleactivity onCreate"); I++;findview (); test_life_cycle_ Btn1.setonclicklistener (New Onclicklistener () {Intent Intent = new Intent (Testlifecycleactivity.this, Testlifecycleactivity2.class); @Overridepublic void OnClick (view view) {TestLifeCycleActivity.this.startActivity ( Intent);}});} private void Findview () {test_life_cycle_btn1 = (Button) Findviewbyid (R.ID.TEST_LIFE_CYCLE_BTN1);} @Overrideprotected void OnDestroy () {Super.ondestroy (); SYSTEM.ERR.PRINTLN (i + "," + "testlifecycleactivity OnDestroy"); i++;} @Overrideprotected void OnPause () {super.onpause (); SYSTEM.ERR.PRINTLN (i + "," + "testlifecycleactivity onPause"); i++;} @Overrideprotected void Onrestart () {Super.onrestart (); SYSTEM.ERR.PRINTLN (i + "," + "testlifecycleactivity Onrestart"); i++;} @Overrideprotected void Onresume () {super.onresume (); SYSTEM.ERR.PRINTLN (i + "," + "testlifecycleactivity onresume"); i++;} @Overrideprotected void OnStart () {Super.onstart (); SYSTEM.ERR.PRINTLN (i + "," + "testlifecycleactivity OnStart"); i++;} @Overrideprotected void OnStop () {super.onstop (); SYSTEM.ERR.PRINTLN (i + "," + "testlifecycleactivity onStop"); i++;}}

TestLifeCycleActivity2. java

Package Com.example.helloworld;import Android.app.activity;import Android.os.bundle;public class TestLifeCycleActivity2 extends Activity {private int i = 1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {s Uper.oncreate (savedinstancestate); Setcontentview (R.layout.test_life_cycle2); SYSTEM.ERR.PRINTLN (i + "," + "TestLifeCycleActivity2 onCreate"); i++;} @Overrideprotected void OnDestroy () {Super.ondestroy (); SYSTEM.ERR.PRINTLN (i + "," + "TestLifeCycleActivity2 OnDestroy"); i++;} @Overrideprotected void OnPause () {super.onpause (); SYSTEM.ERR.PRINTLN (i + "," + "TestLifeCycleActivity2 onPause"); i++;} @Overrideprotected void Onrestart () {Super.onrestart (); SYSTEM.ERR.PRINTLN (i + "," + "TestLifeCycleActivity2 Onrestart"); i++;} @Overrideprotected void Onresume () {super.onresume (); SYSTEM.ERR.PRINTLN (i + "," + "TestLifeCycleActivity2 onresume"); i++;} @Overrideprotected void OnStart () {Super.onstart (); SYSTEM.ERR.PRINTLN (i + "," + "TestLifeCycleActivity2 OnStart"); i++;} @Overrideprotected void OnSTop () {super.onstop (); SYSTEM.ERR.PRINTLN (i + "," + "TestLifeCycleActivity2 onStop"); i++;}}

Testlifecycleactivity the layout file. There is only one button. Go to sub-interface:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <button android:id= "@+id/test_life_cycle_btn1" android:layout_width= "Fill_    Parent "android:layout_height=" wrap_content "android:text=" Test activity life cycle "/></linearlayout>


TestLifeCycleActivity2 layout files such as the following. No content:

<?xml version= "1.0" encoding= "Utf-8"?

><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match _parent " android:layout_height=" match_parent " android:orientation=" vertical "> </linearlayout >


Register the main interface and sub-interface in Androidmanifest.xml:

<?xml version= "1.0" encoding= "Utf-8"?

><manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.example.helloworld" Android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "ten" Android : targetsdkversion= "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launch Er "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity and Roid:name= "com.example.helloworld.TestLifeCycleActivity" android:label= "@string/app_name" > <i ntent-filter> <action android:name= "Android.intent.action.MAIN"/> <category and Roid:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <a Ctivity android:name= "Com.example.helloworld.TestLifeCycleActivity2" > </activity> </appli Cation></manifest>


The test process, click on the main interface button, go to the sub-interface, and then click Back to navigation, back to the main screen. The various life cycle methods run such as the following:

05-14 10:54:19.012:w/system.err (1862): 1, testlifecycleactivity oncreate05-14 10:54:19.012:w/system.err (1862): 2, Testlifecycleactivity onstart05-14 10:54:19.041:w/system.err (1862): 3, Testlifecycleactivity OnResume--above three initialization of the main interface 05-14 10:54:23.481:w/system.err (1862): 4, Testlifecycleactivity onPause--click the button and you will be transferred to the sub-interface. Pause the main interface first 05-14 10:54:25.241:w/system.err (1862): 1, TestLifeCycleActivity2 onCreate 05-14 10:54:25.251:w/system.err (1862 ): 2, TestLifeCycleActivity2 onstart05-14 10:54:25.281:w/system.err (1862): 3, TestLifeCycleActivity2 Onresume--initialization sub-interface 。 The sub-interface is now rendered. Occupy the front of the screen 05-14 10:54:27.551:w/system.err (1862): 5, Testlifecycleactivity onStop--The main interface is not visible, run the main interface of the Stop method 05-14 10:55:11.7 42:w/system.err (1862): 4, TestLifeCycleActivity2 onPause--When you click Back to navigate, pause the sub-interface 05-14 10:55:11.802:w/system.err (1862): 6, Tes Tlifecycleactivity Onrestart--restart method for running the main interface 05-14 10:55:11.802:w/system.err (1862): 7, Testlifecycleactivity OnStart--Start method running the main interface 05-14 10:55:11.813:W/SYSTEM.ERR (1862): 8, Testlifecycleactivity onresume--The Onresume method that runs the main interface, once again starts to render the main interface, when the main interface is at the front of the screen 05-14 10:55:13.354:w/ System.err (1862): 5, TestLifeCycleActivity2 OnStop--The sub-interface is not visible. Run sub-Interface OnStop method 05-14 10:55:13.362:w/system.err (1862): 6, TestLifeCycleActivity2 OnDestroy-Destroy sub-interface

Test process 2--Click on the main interface button, will pop up the sub-interface. At this point go back to navigation, until Exit HelloWorld program, return to the phone, the main interface and sub-interface of the various life cycle of the method run such as the following:

05-14 10:56:50.281:w/system.err (1862): 1, testlifecycleactivity oncreate05-14 10:56:50.301:w/system.err (1862): 2, Testlifecycleactivity onstart05-14 10:56:50.391:w/system.err (1862): 3, Testlifecycleactivity onResume--Main interface initialization rendering 05-1 4 10:56:55.331:w/system.err (1862): 4, Testlifecycleactivity onPause--sub-interface will occupy the phone screen, pause the main interface 05-14 10:56:56.411:w/system. Err (1862): 1, TestLifeCycleActivity2 onCreate 05-14 10:56:56.421:w/system.err (1862): 2, TestLifeCycleActivity2 Onstart05-14 10:56:56.441:w/system.err (1862): 3, TestLifeCycleActivity2 onresume--sub-interface rendering 05-14 10:56:58.051:w/ System.err (1862): 4, TestLifeCycleActivity2 onPause--The main interface will occupy the phone screen. Pause sub-Interface 05-14 10:56:58.261:w/system.err (1862): 5, testlifecycleactivity onresume-user Click Back to navigate to the main interface, run the main interface Onresume.     Because the sub-interface does not have time to occupy the phone screen, the main interface is still visible at this point, it will not run its OnStop method 05-14 10:56:59.111:w/system.err (1862): 5, TestLifeCycleActivity2 OnStop --sub-interface is not visible, run OnStop method 05-14 10:56:59.111:w/system.err (1862): 6, TestLifeCycleActivity2 OnDestroy-Destroy sub-interface 05-14 10:56:59.521:w/system.err (1862): 6, Testlifecycleactivity onPause--The user clicks multiple times to return navigation, returns to the main interface, then exits the main interface, to the mobile phone desktop, you need to pause the main interface 05-14 10:57:04.571:w/system.err (1862): 7, Testlifecycleactivity onStop--then the main interface is not visible. Stop the main interface 05-14 10:57:04.571:w/system.err (1862): 8, Testlifecycleactivity OnDestroy--then destroy the main interface



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.