Android activity cycle

Source: Internet
Author: User

Go directlyCode:

First Class: testactivity

Package Feb. hxy; import Feb. hxy. r. string; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class testactivity extends activity {private button; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); system. out. println ("testactivity. oncreate "); button = (button) findviewbyid (R. id. anotheractivity); button. settext (string. anotheractivity); button. setonclicklistener (New button. onclicklistener () {public void onclick (view v) {intent = new intent (); intent. setclass (testactivity. this, anothpolicfti.pdf. class); startactivity (intent); testactivity. this. finish () ;}}) ;}public void onstart () {system. out. println ("testactivity. onstart "); super. onstart ();} public void onresume () {system. out. println ("testactivity. onresume "); super. onresume ();} public void onpause () {system. out. println ("testactivity. onpause "); super. onpause ();} public void onstop () {system. out. println ("testactivity. onstop "); super. onstop ();} public void ondestroy () {system. out. println ("testactivity. ondestory "); super. ondestroy ();} public void onrestart () {system. out. println ("testactivity. onrestart "); super. onrestart ();}}

The second category: anoth1_fti.pdf

Package Feb. hxy; import Feb. hxy. r. string; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class anoth?fti=extends activity {private button; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); system. out. println ("anoth?fti=. oncreate "); button = (button) findviewbyid (R. id. anotheractivity); button. settext (string. back); button. setonclicklistener (New button. onclicklistener () {public void onclick (view v) {intent = new intent (); intent. setclass (anothpolicfti.pdf. this, testactivity. class); startactivity (intent); // anoth?fti=. this. finish () ;}}) ;}public void onstart () {system. out. println ("anoth?fti=. onstart "); super. onstart ();} public void onresume () {system. out. println ("anoth?fti=. onresume "); super. onresume ();} public void onpause () {system. out. println ("anoth?fti=. onpause "); super. onpause ();} public void onstop () {system. out. println ("anoth?fti=. onstop "); super. onstop ();} public void ondestroy () {system. out. println ("anoth?fti=. ondestory "); super. ondestroy ();} public void onrestart () {system. out. println ("anoth?fti=. onrestart "); super. onrestart ();}}

Run the command in the simulator and check the ddms. The following content is displayed first:

ClickProgramThe buttons in are as follows:

Also, click the button component in the second activity, as shown below:

Note:The ondestroy () method of anotheractivity is not called, because anotheractivity has commented on:

 
// Anoth?fti=. this. Finish ();

It can be seen that ondestroy () is called when finish () is called (). Destroy the activity.

Click the return key on the simulator. The result is as follows:

At this time, the onrestart () method of anotheractivity is called.

Remove the annotation in anotheractivity and follow the preceding steps. The running result is as follows:

Download complete code:Testandroid (Note: This program uses a simulator of 2.2)

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.