Android activity The life cycle of the _android switch

Source: Internet
Author: User

Objective

In development often to deal with the screen switch, how to deal with the first look at the life cycle

Statement

The activity requires a callback of two functions when the activity is switched, so this two function is temporarily viewed as part of the lifecycle of the activity's screen switch, as follows

Onsaveinstancestate (Bundle outstate): The activity is about to be destroyed when the data is saved
onrestoreinstancestate (Bundle savedinstancestate): Extract data when activity is rebuilt or recovered

Toggle life cycle on the screen

1, start the program into the activity interface

2,

2. Rotate the screen

3. Rotate the screen again

4 set in Androidmanifest.xml

Android:configchanges= "Orientation|screensize", the cut screen will not recall each lifecycle, will only execute the Onconfigurationchanged method

Attention:

Minisdkversion is greater than or equal to 13 times: android:configchanges= "orientation" or android:configchanges= "Orientation|keyboardhidden" Re-invoke each life cycle

Minisdkversion less than 13 times:

(1) Do not set the activity of the android:configchanges, the screen will recall the life cycle, cut the horizontal screen will be executed once, cut the vertical screen will be executed twice

(2) When setting the activity's android:configchanges= "orientation", the cut screen will recall each lifecycle, cut horizontally, vertical screen will only be executed once

(3) When setting the android:configchanges= "Orientation|keyboardhidden" of an activity, the cut screen does not recall the individual lifecycle, only the Onconfigurationchanged method is executed

5, screen switching to avoid, re-moving activity life cycle

From the above screen switching lifecycle you can see that each switch is recreated, in order to unnecessary trouble such as video playback screen rotation, etc., to avoid the recurrence of life cycle is a better solution

(1) Android 2.3 Previous version android:configchanges= "Orientation|keyboardhidden"

(2) after the Android 3.0 version android:configchanges= "Orientation|screensize"

Screen settings

Android screen switching in the mobile phone development is more common, many software in the development process in order to avoid the screen switch caused by the unnecessary trouble, the general ban on the screen switch.

The Android:screenorientation attribute value in the activity is set in the Androidmanifest.xml to implement.

(1) Vertical screen: android:screenorientation= "Portrait"

(2) Horizontal screen: android:screenorientation= "Landscape"

Second, in the Java code by similar code to set up (this method is not recommended, in the large app in different directions will be slow to boot)

(1) Vertical screen: setrequestedorientation (activityinfo.screen_orientation_portrait)

(2) Horizontal screen: Setrequestedorientation (Activityinfo.screen_orientation_landscape)

Third, if you want to completely prohibit the flip, ignore the gravitational induction of the switch, (the simulator does not work, in the true machine is correct)

(1) Ignoring gravity: android:screenorientation= "Nosensor"

Horizontal screen identification

First, in the onconfigurationchanged to judge, in order to onconfigurationchanged in the monitoring screen direction of changes in the effective need for the following conditions

(1) Androidmanifest.xml Additional permissions: <uses-permission android:name= "Android.permission.CHANGE_CONFIGURATION" ></ Uses-permission>

(2) The Minisdkversion and Targetsdkversion properties set in Androidmanifest.xml are greater than or equal to 13

(3) Increase in androidmanifest.xml activity: android:configchanges= "Keyboard|screensize|orientation|layoutdirection"

(4) Judgment in onconfigurationchanged (Configuration newconfig)

@Override public
void onconfigurationchanged (Configuration newconfig) {
super.onconfigurationchanged ( Newconfig);
if (newconfig.orientation = 1)//Vertical screen 
if (newconfig.orientation = = 2)/horizontal screen
}

Second, because when the screen changes to a horizontal screen, the system will call the activity's OnCreate method to check the current direction in the OnCreate, and then allow your setcontentview to load different layout XML.

if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {
Log.i ("info", "Landscape"); Horizontal screen
} else if (This.getresources (). GetConfiguration (). Orientation ==configuration.orientation_portrait) {
LOG.I ("Info", "Portrait"); Vertical Screen
}

Note: This method is not set in the Androidmanifest.xml onconfigurationchanged in order to go back to life cycle

Toggle layout File settings on the screen

If you want to allow the software to switch between the two screens, because the width of the screen will be converted, it may require different layouts. You can switch layouts in the following ways

(1) in the Res directory to establish Layout-land and Layout-port directory, the corresponding layout file name unchanged, such as Main.xml. Layout-land is the horizontal screen of the Layout,layout-port is the layout of the vertical screen, the other without the tube, the simulator will automatically find.

(2) In the above screen identification, if the vertical screen changes, in the OnCreate () or onconfigurationchanged () to determine the direction, you can in the corresponding method to Setcontentview to load different layout XML layout files

Saving and reading of data in the screen switching

In addition, each screen switch in Android will restart the activity, so you should save the current active state before the activities are destroyed, and load the configuration when it is again create, so that the game in progress will not be restarted automatically!

Activity data Preservation

(1) If the destory of the activity or the rotation of the screen is destroyed and recreated because of the tight system resources, the system will have a record of the activity when the user returns to the activity, The system uses the saved record data (instance state), which is the key-value pairs that is stored in the bundle object, and the system uses bundle to save the information by default

(2) in order to save additional data to instance state, rewrite write this callback function onsaveinstancestate (Bundle outstate), the system will pass the Destory object when the activity is abnormally Bundle So that we can add additional information to the bundle and save it to the system. If the system wants to recreate the activity instance after the activity is destory, the previous bundle object will be passed to you.

(3) The activity begins to stop, the system invokes Onsaveinstancestate (Bundle outstate), and the activity can save the state information with a set of key-value pairs. This method saves the state information of the activity view by default, such as the text in the EditText component or the sliding position of the ListView

Activity data Recovery

(1) When the activity is reconstructed from the destory, we can recover the saved state from the bundle of the activity passed by the system. Both the OnCreate () and the Onrestoreinstancestate () callback methods receive the same bundle, which contains the same instance state information.

(2) since the OnCreate () method is invoked the first time a new activity instance is created and the instance being destory before it is recreated, we must detect whether it is null before attempting to read the Bundle object. If it is null, the system creates a new instance of the activity, rather than resuming the activity that was previously destory.

(3) You can also choose to implement Onrestoreinstancestate () instead of recovering the data in the OnCreate method. The Onrestoreinstancestate () method executes after the OnStart () method. The system calls Onrestoreinstancestate () only if there is state information that needs to be recovered, and therefore does not need to check whether the Bundle is null.

The above is a small set of Android activity to introduce the life cycle of the relevant knowledge, hope to help everyone!

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.