Android Development Notes-some things about the activity life cycle

Source: Internet
Author: User

It is important to be aware of what has been learned before, and every time you look back, you have a different understanding of it, or you understand it more deeply, or you understand it more thoroughly.

The recent review of the activity's life cycle, referring to relevant books and official documents, has made a significant improvement in previous cognition and recorded something here.

Let's take a look at the classic life cycle diagram:

Simple overview:

1. The OnCreate method is executed when an activity is called for the first time

2. The OnStart method is called when the activity is in a visible state

3. The Onresume method is called when the activity can get the user focus

4. When the activity is blocked, the OnPause method is called, and the occlusion end returns the Onresume method that the user gets the focus state.

5. When the activity is in an invisible state, the OnStop method is called, and if the activity is not destroyed, the Onrestart method is called again and the OnStart method is returned.

7. The Ondestory method is called when the activity is destroyed

To test the segment code:

1  Packagecom.lcw.rabbit.activity;2 3 Importandroid.app.Activity;4 Importandroid.content.res.Configuration;5 ImportAndroid.os.Bundle;6 ImportAndroid.util.Log;7 8  Public classMainactivityextendsActivity {9     Ten     PrivateString MInfo; One  A @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); -LOG.I ("Rabbit", "onCreate------"); -minfo= "I Am Rabbit"; -     } +  - @Override +     protected voidOnStart () { A         Super. OnStart (); atLOG.I ("Rabbit", "onStart------"); -     } -  - @Override -     protected voidOnresume () { -         Super. Onresume (); inLOG.I ("Rabbit", "onresume------"); -     } to  + @Override -     protected voidOnPause () { the         Super. OnPause (); *LOG.I ("Rabbit", "onPause------"); $     }Panax Notoginseng  - @Override the     protected voidOnStop () { +         Super. OnStop (); ALOG.I ("Rabbit", "onStop------"); the     } +  - @Override $     protected voidOnDestroy () { $         Super. OnDestroy (); -LOG.I ("Rabbit", "OnDestroy------"); -     } the  - @OverrideWuyi     protected voidOnrestart () { the         Super. Onrestart (); -LOG.I ("Rabbit", "onrestart------"); Wu     } -  About @Override $     protected voidonsaveinstancestate (Bundle outstate) { -         Super. Onsaveinstancestate (outstate); -Outstate.putstring ("Info", mInfo); -LOG.I ("Rabbit", "onsaveinstancestate------Info:" +mInfo); A     } +  the @Override -     protected voidonrestoreinstancestate (Bundle savedinstancestate) { $         Super. Onrestoreinstancestate (savedinstancestate); theString info=savedinstancestate.getstring ("info"); theLOG.I ("Rabbit", "onrestoreinstancestate------Info:" +info); the     } the      - @Override in      Public voidonconfigurationchanged (Configuration newconfig) { the         Super. onconfigurationchanged (newconfig); theLOG.I ("Rabbit", "onconfigurationchanged------"); About     } the      the @Override the      Public voidOnwindowfocuschanged (BooleanHasfocus) { +         Super. onwindowfocuschanged (hasfocus); -LOG.I ("Rabbit", "onwindowfocuschanged------"); the     }Bayi  the}
View Code

Test result diagram:

1, when the activity enters the time, in turn calls Oncreate->onstart->onresume, enters the running state.

2, when the phone press home button back to the phone desktop, in turn, call Onpause->onstop, into a stagnant state.

3. When you enter activity again, call Onrestart->onstart->onresume in turn and enter the running state again.

4. When activity is overwritten or locked by other activity, the system calls the OnPause method, pausing the execution of the current activity.

5, when the activity from the covered state back to the foreground or unlock screen, the system will call the Onresume method, again into the running state.

6, when pressing the phone back button, call Onpause->onstop->ondestroy in turn, end the current activity.

The above is a preliminary demonstration of the activity life cycle, but it's not enough to just know about it, and to make the program more robust and user-friendly, we need to focus on some of the details.

Here we need extra attention. 3 methods: Onwindowfocuschanged, Onsaveinstancestate, onrestoreinstancestate

1. onwindowfocuschanged: Called when the activity window Gets or loses focus;

(1) first presented in front of the user at the time of creation;

(2) Current activity is covered by other activity;

(3) The current activity to move to other activity or press the home button back to the main screen, the user retired from the current activity.

When activity is created, onwindowfocuschanged is called after Onresume, and when activity is overwritten or retired from the background or the current activity exits, Onwindowfocuschanged is called after OnPause.

  

2, Onsaveinstancestate:

(1) After the activity is overwritten or retired from the background, the system resources are not enough to kill it, this method will be called;

(2) This method is called when the user changes the orientation of the screen;

(3) This method is called when the current activity jumps to another activity or presses the home key to return to the main screen.

In the first case, we cannot guarantee when the system will be dispatched according to the resource intensity;

The second is the screen flipping direction, the system first destroys the current activity, and then rebuild a new, call this method, we can save some temporary data;

In the third case, the system calls this method to save the state of each view component in the current window. The order in which the onsaveinstancestate is called is after OnPause.

3, Onrestoreinstancestate:

(1) After the activity is overwritten or retired from the background, the system resources are not enough to kill, and then the user returned to the activity, this method will be called;

(2) When the user changes the screen orientation, this method is called during the rebuild process. We can override this method so that some temporary data can be recovered.

The order in which the onrestoreinstancestate is called is after OnStart.

Let's simulate the use of onsaveinstancestate and onrestoreinstancestate:

First I define a member variable and assign a value in the OnCreate method:

    Private String MInfo;    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {        super  . OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        LOG.I ("Rabbit", "onCreate------");        MInfo= "I Am Rabbit";    }

The data are saved and taken out separately in the Onsaveinstancestate and Onrestoreinstancestate methods:

@Overrideprotected voidonsaveinstancestate (Bundle outstate) {Super. Onsaveinstancestate (outstate); Outstate.putstring ("Info", MInfo); LOG.I ("Rabbit", "onsaveinstancestate------Info:" +mInfo); } @Overrideprotected voidonrestoreinstancestate (Bundle savedinstancestate) {Super. Onrestoreinstancestate (savedinstancestate); String Info=savedinstancestate.getstring ("Info"); LOG.I ("Rabbit", "onrestoreinstancestate------Info:" +info); }

Then flip the screen to see the log print effect:

From the log printing results, we can clearly see the program execution process:

1. Our activity was destroyed and rebuilt, Onsaveinstancestate was executed after OnPause, and the value of our variable was maintained.

2. The Onrestoreinstancestate method was executed before the activity was reconstructed to get the focus (Onresume), and the value of the variable we saved previously was removed.

Knowing this process, we can use Onsaveinstancestate and onrestoreinstancestate to save some temporary variables, such as button states, flag bits and so on.

Coping strategies for changing the screen orientation of activity will be rebuilt:

1, designated as vertical screen: In Androidmanifest.xml to the specified activity settings:

android:screenorientation= "Portrait"

Or specify in the OnCreate method:

Setrequestedorientation (activityinfo.screen_orientation_portrait);  // Vertical Screen  

2, designated as a horizontal screen: In Androidmanifest.xml to the specified activity settings:

android:screenorientation= "Landscape"

Or specify in the OnCreate method:

// Horizontal Screen  

3, lock screen Although we can achieve the desired effect, but is not a good practice, in order to avoid this destruction of the reconstruction process, we can in the Androidmainfest.xml to the corresponding <activity> configuration:

android:configchanges= "Orientation"

If it is Android4.0, it is:

Android:configchanges= "Orientation|keyboardhidden|screensize"

Then we rewrite the Onconfigurationchanged method in the activity:

    @Override    publicvoid  onconfigurationchanged (Configuration newconfig) {         Super . onconfigurationchanged (newconfig);        LOG.I ("Rabbit", "onconfigurationchanged------");    }

Look at the execution effect:

In this way, the activity will not be destroyed when the screen is flipped and rebuilt, but the Onconfigurationchanged method is called.

Android Development Notes-some things about the 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.