SoFunction
Updated on 2025-04-06

Android Activity Lifecycle of horizontal and vertical screen switching

Preface

In development, we often need to deal with horizontal and vertical screen switching. How to deal with it depends on the life cycle first

Statement

Activity requires two functions to be called back when switching horizontal and vertical screens, so here we temporarily regard these two functions as part of the life cycle of Activity horizontal and vertical screens. These two functions are as follows

onSaveInstanceState(Bundle outState) :Activity Save data when it is about to be destroyed
onRestoreInstanceState(Bundle savedInstanceState) : Activity Retrieve data during reconstruction or recovery

Switch life cycle with horizontal and vertical screens

1. Start the program and enter the Activity interface

2、

2. Rotate the screen

3. Rotate the screen again

4 Set in

android:configChanges="orientation|screenSize", screen-slicing will not re-call each life cycle, it will only execute the onConfigurationChanged method

Notice:

When MiniSdkVersion is greater than or equal to 13: android:configChanges="orientation" or android:configChanges="orientation|keyboardHidden" re-calls each life cycle

When MiniSdkVersion is less than 13:

(1) When the android:configChanges of the Activity is not set, the screen-cutting will call each life cycle again, and it will be executed once when the horizontal screen is cut, and it will be executed twice when the vertical screen is cut.

(2) When setting the activity's android:configChanges="orientation", the screen-cutting will still call each life cycle again, and it will only be executed once when cutting horizontal and vertical screens.

(3) When setting the activity's android:configChanges="orientation|keyboardHidden", the screen-slicing will not re-call each life cycle, and will only execute the onConfigurationChanged method

5. Avoid screen switching and retrace the Activity life cycle

From the above screen switching life cycle, it can be seen that each switch is recreated. For unnecessary troubles, such as video playback screen rotation, avoiding retracement of life cycle is a better solution.

(1) Versions before android 2.3 android:configChanges="orientation|keyboardHidden"

(2) Version after Android 3.0 android:configChanges="orientation|screenSize"

Horizontal and vertical screen settings

Android horizontal and vertical screen switching is common in mobile phone development. In order to avoid unnecessary troubles caused by horizontal and vertical screen switching during the development process, many software usually prohibit the switching of horizontal and vertical screens.

1. Set the Android:screenOrientation property value in the activity to achieve it.

(1) Perpendicular screen: android:screenOrientation="portrait"

(2) Horizontal screen: android:screenOrientation="landscape"

2. Set it in Java code by following code (This method is not recommended, it will be slow when starting in different directions of large apps)

(1) Vertical screen: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

(2) Horizontal screen: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

3. If you want to completely prohibit flipping and ignore the switching caused by gravity sensing (it doesn't work on the simulator, it is correct on the real machine)

(1) Ignore gravity: android:screenOrientation="nosensor"

Horizontal and vertical screen identification

1. In onConfigurationChanged, the following conditions are required for onConfigurationChanged to be effective in listening for the change in the direction of the monitor screen.

(1) Add permissions: <uses-permission android:name=".CHANGE_CONFIGURATION"></uses-permission>

(2) The MiniSdkVersion and TargetSdkVersion properties set are greater than or equal to 13

(3) Add: android:configChanges="keyboard|screenSize|orientation|layoutDirection" in the Activity

(4) Make a judgment in onConfigurationChanged(Configuration newConfig)

@Override
public void onConfigurationChanged(Configuration newConfig) {
(newConfig);
if( == 1)//Perpendicular screenif( == 2)// Horizontal screen}

2. Because when the screen becomes horizontal, the system will re-call the onCreate method of Activity. You can check the current direction in onCreate, and then let your setContentView load different layout xml.

if (().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
("info","landscape"); // Horizontal screen} else if(().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
("info","portrait"); // Vertical screen}

Note: This method is to retrace the life cycle without setting onConfigurationChanged in

Switch layout file settings on horizontal and vertical screens

If you want the software to switch between horizontal and vertical screens, the height and width of horizontal and vertical screens may be converted, and different layouts may be required. You can switch layouts by following the following methods

(1) Create layout-land and layout-port directories in the res directory, and the corresponding layout file names remain unchanged, for example. layout-land is the layout for horizontal screen, and layout-port is the layout for vertical screen. Don’t worry about other things, the emulator will automatically search for them.

(2) In the horizontal and vertical screen identification above, if the horizontal and vertical screen changes, determine the direction in onCreate() or onConfigurationChanged(), you can resetContentView in the corresponding method to load different layout xml layout files

Switch data storage and reading on horizontal and vertical screens

In addition, every screen switching in Android will restart the Activity, so the current activity status should be saved before the Activity is destroyed, and the configuration should be loaded when the Activity Creates again, so that the in-progress game will not automatically restart!

Activity Data Saving

(1) If the Destory of the Activity or the screen is destroyed and recreated due to the shortage of system resources, the system will have records of the Activity when the user returns to this Activity. The system will use the saved record data (instance state). It is some key-value pairs stored in the Bundle object. The system uses Bundle to save information by default.

(2) In order to save more extra data to instance state, we need to rewrite the callback function onSaveInstanceState(Bundle outState). The system will pass the Bundle object when the Activity is exception Destory, so that we can add additional information to the Bundle and save it to the system. If the system wants to recreate this Activity instance after the Activity is Destory, the previous Bundle object will be passed to your activity (system)

(3) Activity starts stop, the system will call onSaveInstanceState(Bundle outState) . Activity can use a collection of key-value pairs to save state information. This method will save the status information of the Activity view by default, such as text in the EditText component or the sliding position of the ListView.

Activity Data Recovery

(1) When the Activity is rebuilt from the Destory, we can restore the saved state from the Bundle of the Activity passed by the system. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle, which contains the same instance status information.

(2) Since the onCreate() method will be called when the first time a new Activity instance is created and the instance that was destory was recreated, we must check whether it is null before trying to read the Bundle object. If it is null, the system creates a new Activity instance, rather than restores the Activity that was previously destory.

(3) You can also choose to implement onRestoreInstanceState() instead of recovering data in the onCreate method. The onRestoreInstanceState() method is executed after the onStart() method. The system will call onRestoreInstanceState() only when there is status information that needs to be restored, so there is no need to check whether the Bundle is null.

The above is the relevant knowledge about the life cycle of Android Activity horizontal and vertical screen switching that the editor introduced to you. I hope it will be helpful to everyone!