Detailed Android Activity life cycle

Source: Internet
Author: User
Tags call back home screen

Describe the activity life cycle in detail in the following areas:

 1. Go back to the main screen and then open and exit the program.

2. When the screen rotates.

3. Open other activity situations.

4. Open a case of layou transparent activity.

Source code:myapplication.7z

Let's start with a life cycle map of activity. The town building.

  

Create a new project, implement the life cycle of all methods, just add a printout in the method, so that we can clearly see

The life cycle callback method that is currently being performed by the activity.

@Overrideprotected voidOnStart () {System.out.println ("OnStart"); Super. OnStart (); } @Overrideprotected voidOnresume () {System.out.println ("Onresume"); Super. Onresume (); } @Overrideprotected voidOnPause () {System.out.println ("OnPause"); Super. OnPause (); } @Overrideprotected voidOnStop () {System.out.println ("OnStop"); Super. OnStop (); } @Overrideprotected voidOnrestart () {System.out.println ("Onrestart"); Super. Onrestart (); } @Overrideprotected voidOnDestroy () {System.out.println ("OnDestroy"); Super. OnDestroy (); }

When we first ran our program, the app went to the main page and looked at the output of Logcat: OnCreate, OnStart, Onresume methods were executed.

1. Return to the home screen.

Let's press the home button to go back to the home screen and take a look at the action it performs.

The OnPause and OnStop methods are implemented. Click on our app to go to the main program and find it performed Onrestart, OnStart and Onresume.

We didn't execute the Onrestart method the first time we ran the app. And we're looking at the official documentation. Once the activity is stopped and activated again

will be called.

Called after your activity had been stopped, prior to it being started again. Always followed by OnStart ()

We click the Close app to see what lifecycle functions it will call back.

It then executes the OnStop and Ondestory methods. So far we have gone through one of the simplest life cycle processes.

2. When rotating the screen.

Run our application, and call Create,start the first time you open it. and resume

Next we'll spin the screen to see what happens.

Suddenly. Called a lot. It is to shut down the activity and re-start the activity again. Here's a note, if our program

To support screen rotation, I'm going to call the Onsaveinstancestate () method to save the current state of the data and restore it in the OnCreate method.

The value of these states.

3. Open a different activity.

Modify one of our programs, add a button to the main page, and add an activity.

    <button        android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content"         Android:text= "New button"        android:id= "@+id/button"        Android:layout_below = "@+id/textview"        android:layout_centerhorizontal= "true"        />

Click on the button to open our newly added activity

        Final Button BTN = (Button) Findviewbyid (R.id.button);        Btn.setonclicklistener (new  View.onclicklistener () {            @Override            public  void  OnClick (View v) {                new Intent (mainactivity.  this, Messagetip. class );                StartActivity (intent);            }        });

Run our app. or the same as before. Call OnCreate (), OnStart (), and Onresume.

Then click on our button on the main page to open another activity.

Press the Back button to see it.

It will call Onrestart (), OnStart (), Onresume. That's the way we talk. 1. Go to the main screen and return to the same.

4. Open the transparent activity.

Transparent activity We do a simple, add a line code directly in the Androidmanifest.xml

  

Run our app again. Call those three methods.

Click the button on the home page to open the transparent activity. Look at what functions are called.

Only the onpause is called. Click Back

The Onresume () method is called.

found that no transparent activity was turned on, the original acitvity did not call the OnStop () method to close the transparent activity. The original activity did not call Onrestart ()

and OnStart () method.

Ok here, the life cycle of activity is very important! The life cycle of activity is very important! The life cycle of activity is very important!

Important thing to say three times!!

Source code:myapplication.7z

Detailed Android Activity life cycle

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.