Life cycle diagram of Android

Source: Internet
Author: User

The activity is the Tangram of Android, because if you compare all of Android's interfaces to a picture that uses tangram, then each activity is equivalent to a tangram. This tangram has nothing to do with the application of his package. Any application can take out their own tangram for others to assemble their pictures, they can also use other applications of Tangram to assemble their own pictures.

Now let's start our Tangram journey.

This topic summarizes the life cycle of the activity.

Well, first insert a picture in the document. This picture is unavoidable.

Here we take a practical example to explain the life cycle of the activity in the above diagram.

    we create a new project and build two activity,activity1 and Activity2. I use the tool is IntelliJ idea, (here or praise the idea of humanization, Creating an activity in Eclipse also requires a manual registration in the Androidmanifest.xml, but with idea, you will find that the new activity has been automatically registered in Androidmanifest.xml. The code for the two activity is as follows: Java code  /**activity1 code */    public class activity1 extends  Activity {             private  static final string tag =  "Activity1";               @Override              Public void oncreate (bundle savedinstancestate)  {                LOG.I (tag,  "activity1 oncreate called!");                 super.oncreate ( savedinstancestate);                 setcontentview (R.layout.main);                  }               @Override              protected void onstart ()  {                 LOG.I (tag,  "activity1 onstart called!");                 super.onstart ();              }                   @Override              protected void onrestart ()  {                  LOG.I (tag,  "activity1 onrestart called!");                 super.onrestart ();              }               @Override              protected void onresume ()  {                LOG.I (tag,  "activity1 onresume called!");                super.onresume ();              }             @Override            protected void  onpause ()  {               LOG.I (tag,  "activity1 onpause called!");                super.onpause ();              }                 @Override                protected void onstop ()  {                 LOG.I (tag,  "activity1 onstop called!");                 super.onstop ();              }               @Override              protected void ondestroy ()  {               log.i (tag,  "Activity1 onDestroy  called! ");                super.ondestroy ();               }               /** when clicking on the screen, enter activity2*/               @Override               public boolean ontouchevent (motionevent event)  {                  intent intent = new intent ( This, activity2.class);                  startactivity (Intent);              &nBsp;   return super.ontouchevent (event);              }     }      /**activity1 code */     public class activity2 extends activity {              private static final String TAG =  "Activity2 ";              @Override              public void oncreate (bundle savedinstancestate)   {               log.i (TAG, ) Activity2 oncreate called! ");                     Setcontentview (r.layout.main2);          &NBSp;      super.oncreate (savedinstancestate);                                 }             @ override            protected void onstart ()  {                log.i (TAG,   "activity2 onstart called!");                 super.onstart ();              }                   @Override              protected void onrestart ()  {                log.i (TAG,  "Activity2  Onrestart called! ");                 super.onrestart ();              }               @Override              protected void onresume ()  {                LOG.I (tag,  "activity2 onresume called!");                super.onresume ();              }             @Override            protected void  onpause ()  {               log.i (TAG,  "Activity2  onpause called! ");                super.onpause ();              }                 @Override                protected void onstop ()  {                 LOG.I (tag,  "activity2 onstop called!");                 super.onstop ();              }               @Override            &NBSp;protected void ondestroy ()  {                LOG.I (tag,  "activity2 ondestroy called!");                super.ondestroy ();               }                   }   

 

Then we add a edittext XML code   <?xml version= "1.0"  encoding= "Utf-8" to the layout file in Activity1?>     <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"                   android:orientation= " Vertical "                   android:layout_width= "Fill_parent"                    android:layout_height= "Fill_parent"              >        <TextView                android:layout_width= "Fill_parent"                  android:layout_height= " Wrap_content "                android:text= "Hello World,  Activity1 "                />             <EditText                 android:layout_width= "Fill_parent"                 android:layout_height= "50SP"                  />    </LinearLayout>   

Process 1

We start the program, enter the Activity1, enter a text in the Activity1 text box, press the Return key, and then look at the debugging information in Logcat.

Java Code 02-27 13:38:27.067:info/activity1 (566): Activity1 onCreate called!      02-27 13:38:27.157:info/activity1 (566): Activity1 OnStart called!         02-27 13:38:27.157:info/activity1 (566): Activity1 onresume called!      02-27 13:41:08.107:info/activity1 (566): Activity1 onpause called!      02-27 13:41:08.897:info/activity1 (566): Activity1 onStop called! 02-27 13:41:08.897:info/activity1 (566): Activity1 OnDestroy called!

From log information we can see that enter the activity and then press the return key to exit. The life cycle of an activity passes through a vertical line along the flowchart. Oncreate->onstart->onpause->onstop->ondestroy. At this time the activity died.

When we enter Activity1 again, we find that the contents of the text box in the Activity1 are empty again, so it can be inferred that the space inside the Activity1 has been destroyed and regenerated.

Process 2

We start the program, enter the Activity1, enter a text within the Activity1, click Activity1, enter Activity2, and then press the return key in Activity2. Or use this process, start the program into the Activity1, press the home button to enter the desktop, and then long press home Click the application re-enter the Activity1.

    then look at the log information. Java code   02-27&NBSP;13:55:35.727:&NBSP;INFO/ACTIVITY1 (: activity1 oncreate called! )       02-27 13:55:35.837: info/activity1:  activity1 onstart  called!       02-27 13:55:35.837: info/activity1 (: ) activity1 onresume called!          02-27 13:55:40.287:  info/activity1 (: activity1 onpause called!  )         02-27 13:55:41.227: info/activity1 (: activity1 onstop called!  )        02-27 14:06:43.017: info/activity1 (634): activity1  onrestart called!       02-27 14:06:43.017: info/activity1 (634):  Activity1 onStart called!       02-27 14:06:43.017: info/activity1 (634): activity1 onresume called!  

From this log information, we see that when we enter Activity2 from Activity1, the process of Activity1 experience is oncreate->onstart->onresume->onpause->onstop- >onrestart->onstart->onresume. This process shows that if the activity is completely obscured by other interfaces, it goes backstage and is not completely destroyed, Onrestart->onstart->onresume, and then resumes when the activity is again entered. This explanation fully explained, chop grass not root, the ashes will also rekindle ... It's kind of evil.

We found that when we returned to Activity1 from Activity2, the text in Activity1 did not change, so it can be inferred that the state of the member after the activity OnStop has not changed.

is not all the interface will be all blocked into the onstop it. Let's look at one of the following processes.

Process 3

Start the program, enter the Activity1, then press the Hang button, enter the lock screen interface, and then return Activity1 from the lock screen interface. We look at the log information.    Java Code 02-27 14:17:45.257:info/activity1 (810): Activity1 onCreate called!    02-27 14:17:45.347:info/activity1 (810): Activity1 OnStart called!        02-27 14:17:45.347:info/activity1 (810): Activity1 onresume called!       02-27 14:17:51.177:info/activity1 (810): Activity1 onpause called! 02-27 14:18:04.757:info/activity1 (810): Activity1 onresume called!

From the log information can be seen, Activity1 was locked screen after all occlusion and did not enter the OnStop, but only into the onpause after the stop, when from the lock screen interface back to Activity1, called Onresume.

Why is the lock screen interface special, because we know that after entering the OnPause, the activity is always active, again Onresume is not what is needed to spend, can be frequent onpause,onresume; OnStart will have a lot more to spend. In order to ensure the return of the lock screen, the original top of the acitivtiy can quickly return to the original state, it is clear that the lock screen interface for special treatment.

Similarly, the EditText text has not changed, so OnPause has not changed the state of the members in the activity. XML code <activity android:name= ". Activity2 "&n

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.