The life cycle of android--activity

Source: Internet
Author: User



One, the demo Test activity life cycle


write two activity:


Package Com.example.activity_04;import Android.os.bundle;import Android.app.activity;import android.content.Intent ; Import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import    Android.widget.button;public class Firstactivity extends Activity {private Button mybutton=null;        @Override protected void OnCreate (Bundle savedinstancestate) {System.out.println ("firstactivity-->oncreate");        Super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_first);        mybutton= (Button) Findviewbyid (R.id.mybutton);    Mybutton.setonclicklistener (New Btnclick ()); } class Btnclick implements onclicklistener{@Overridepublic void OnClick (View arg0) {//TODO self-generated method stub intent Intent=n EW Intent (); Intent.setclass (Firstactivity.this,secondactivity.class);            FirstActivity.this.startActivity (intent);} } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action BaR if it is present.        Getmenuinflater (). Inflate (R.menu.first, menu);    return true; } @Overrideprotected void Onrestart () {//TODO self-generated method stub System.out.println ("Firstactivity-->onrestart"); Super.onrestart ();} @Overrideprotected void OnDestroy () {//TODO self-generated method stub System.out.println ("Firstactivity-->ondestroy"); Super.ondestroy ();} @Overrideprotected void OnPause () {//TODO self-generated method stub System.out.println ("Firstactivity-->onpause"); Super.onpause ();} @Overrideprotected void Onresume () {//TODO self-generated method stub System.out.println ("Firstactivity-->onresume"); Super.onresume ();} @Overrideprotected void OnStart () {//TODO self-generated method stub System.out.println ("Firstactivity-->onstart"); Super.onstart ();} @Overrideprotected void OnStop () {//TODO self-generated method stub System.out.println ("Firstactivity-->onstop"); Super.onstop ()        ;} }



Package Com.example.activity_04;import Android.app.activity;import Android.os.bundle;public class SecondActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {//TODO self-generated method stub System.out.println (" Secondactivity--->oncreate "); Super.oncreate (savedinstancestate);} @Overrideprotected void OnDestroy () {//TODO self-generated method stub System.out.println ("Secondactivity--->ondestroy"); Super.ondestroy ();} @Overrideprotected void OnPause () {//TODO self-generated method stub System.out.println ("Secondactivity--->onpause"); Super.onpause ();} @Overrideprotected void Onrestart () {//TODO self-generated method stub System.out.println ("Secondactivity--->onrestart"); Super.onrestart ();} @Overrideprotected void Onresume () {//TODO self-generated method stub System.out.println ("Secondactivity--->onresume"); Super.onresume ();} @Overrideprotected void OnStart () {//TODO self-generated method stub System.out.println ("Secondactivity--->onstart"); Super.onstart ();} @Overrideprotected void OnStop () {///TODO self-generated method stub System.out.println ("SecOndactivity--->onstop "); Super.onstop ();}} 


Then start the program,



Be able to see the method that the first activity is called on.


< Span style= "FONT-FAMILY:CALIBRI;" >on

< Span style= "FONT-FAMILY:CALIBRI;" >on

< Span style= "FONT-FAMILY:CALIBRI;" >on

called the first time it is created, can be configured with the Layout file

when Ac tivity when we can see it.

when Activity be called when the user focus is available


Then click on the button on the first activiyt to jump to the second activity:





The OnPause method that finds the first activity is called, followed by the initialization of a new method that the activity is called, and the first activity's OnStop method is called after the second activity appears.


onpause

< Span style= "FONT-FAMILY:CALIBRI;" >on

Application launches another activity activity pause method

: usually save current activity

&NBSP;

when Activity The first one is called when it is not visible Activity of the Onstop method, assuming that the new Activity not completely obscuring the first one . Activity , you do not call


Then click Backbutton,





Note that the Onrestart method is called first after the first activity is started.


on Destory

on Restart

destroyed Activity

This method is called in two general cases:

1 , calling Activity of the Finish Method

2 when the system resources are not available

Call Again Paused Activity



Summary:



Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>




The life cycle of the activity call feels similar to the. NET inside page life cycle. It feels simpler than that.








The life cycle of android--activity

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.