"Android Development Learning Notes" "Lesson five" activity life cycle-on

Source: Internet
Author: User

Learn about the seven life cycle functions in activity today:

The first thing to say is that if you add System.out.println ("------") to the code, then how do you see what's in the output?

  

Only "Filter Name" and "by Log Tag" will be entered after opening.

  

When you add a success, you see

  

The main summary of this study is as follows:

1, OnCreate ()

Acitivity is called when it is first created. For setting acitivity layout files, binding button listeners, and other common static operations.

2, OnStart ()

Acitivity is called when the user is visible.

3, Onresume ()

Acitivity Gets the user focus, which is called when the user can manipulate the acitivity.

4, OnPause ()

Called when the application launches other acitivity. Typically used to save data in the current acitivity.

5, OnStop ()

Called when the acitivity is not visible.

6, Onrestart ()

Called when a stopped acitivity is restarted.

7, OnDestroy ()

Called when the Finish () method of the Acitivity or Android system is low on resources.

If this is not a good idea, see below:

First, two activity is created, and the 7 life cycle functions of the two activity are rewritten, and the first activity can invoke the second activity via a button.

  

 Packagecom.example.activitytest;ImportCOM.EXAMPLE.ACTIVITYTEST.R;Importcom.example.activitytest.FirstActivity;Importandroid.app.Activity;ImportAndroid.app.ActionBar;Importandroid.app.Fragment;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.os.Build; Public classFirstactivityextendsActivity { PublicButton MyButton =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_first); System.out.println ("First----onCreate ()"); MyButton=(Button) Findviewbyid (R.id.mybutton); Mybutton.settext ("My button"); Mybutton.setonclicklistener (NewMybuttonlisten ()); }        classMybuttonlistenImplementsOnclicklistener {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent Intent =NewIntent (); Intent.putextra ("Testintent", "Ccyy"); Intent.setclass (firstactivity. This, Secondactivity.class); Firstactivity. This. StartActivity (Intent); }} @Overrideprotected voidOnDestroy () {//TODO auto-generated Method StubSystem.out.println ("first----OnDestroy ()"); Super. OnDestroy (); } @Overrideprotected voidOnPause () {//TODO auto-generated Method StubSystem.out.println ("first----onPause ()"); Super. OnPause (); } @Overrideprotected voidOnrestart () {//TODO auto-generated Method StubSystem.out.println ("first----onrestart ()"); Super. Onrestart (); } @Overrideprotected voidOnresume () {//TODO auto-generated Method StubSystem.out.println ("first----onresume ()"); Super. Onresume (); } @Overrideprotected voidOnStart () {//TODO auto-generated Method StubSystem.out.println ("first----onStart ()"); Super. OnStart (); } @Overrideprotected voidOnStop () {//TODO auto-generated Method StubSystem.out.println ("first----onStop ()"); Super. OnStop (); }}

It's the first activity.

  

 Packagecom.example.activitytest;Importandroid.app.Activity;ImportAndroid.os.Bundle; Public classSecondactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.second); System.out.println ("Second----onCreate ()"); } @Overrideprotected voidOnDestroy () {//TODO auto-generated Method StubSystem.out.println ("second----OnDestroy ()"); Super. OnDestroy (); } @Overrideprotected voidOnPause () {//TODO auto-generated Method StubSystem.out.println ("second----onPause ()"); Super. OnPause (); } @Overrideprotected voidOnrestart () {//TODO auto-generated Method StubSystem.out.println ("second----Onrestart ()"); Super. Onrestart (); } @Overrideprotected voidOnresume () {//TODO auto-generated Method StubSystem.out.println ("second----onresume ()"); Super. Onresume (); } @Overrideprotected voidOnStart () {//TODO auto-generated Method StubSystem.out.println ("second----OnStart ()"); Super. OnStart (); } @Overrideprotected voidOnStop () {//TODO auto-generated Method StubSystem.out.println ("second----onStop ()"); Super. OnStop (); }}

There's a second activity.

Next run the program and look at the information displayed in the log:

1. After launch, the first activity is displayed

2. Call its second activity via a button

The 3.back key returns to the first activity

4. Exit the App

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.