Detailed explanation of horizontal and vertical switching of Android screen

Android screen vertical and horizontal switching

Control the direction of activity in Android, as long as it is in androidmanifest Add a sentence under the corresponding activity node in the XML

Android: screenorientation = "landscape" (landscape is horizontal screen and portal vertical screen).

I've been mixing with this sentence for a year. It's basically OK, but these days I've found that this move is not omnipotent.

In the project, there is a playactivity for playing video horizontally. I set Android: screenorientation = "landscape" as before. There was no problem here. Later, a requirement was added. Click the "comment" button on the screen to pop up a dialog box. I implemented this dialog box with activity. The effect is: the vertical screen and comment interface occupy the middle of the screen, surrounded by translucent.

Something magical happened. After the comment window popped up, I found that the playback interface behind turned into a vertical screen. (as mentioned above, the dialog box only occupies most of the screen temporarily, and the surrounding is transparent, so you can see the back interface playacitivity).

I was depressed when I saw the screen switch to vertical screen. I clearly set Android: screenorientation = "landscape". Why did I switch.

Later, Google and Baidu... Did not find out the reason. Self understanding is like this: Although playacitivity is covered, some are still visible. The top window (dialog box) is set to Android: screenorientation = "portal". There is only one screen, which must be subject to the top window setting.

In Android, when the screen is switched horizontally and vertically, the life cycle of the activity is to reload (indicating that the current activity is destroyed, but the loading is performed again). How can the current activity not be destroyed when the screen is switched horizontally and vertically?

At androidmanifest Set the configchanges property for the activity in XML

Configchanges has the following options:

1. Orientation: the screen rotates vertically and horizontally;

2. Keyboardhidden: the keyboard is displayed or hidden;

3. Fontscale: the user changed the preferred font size

4. Locale: the user has selected different language settings;

5. Keyboard: keyboard type changes, for example, the mobile phone switches from 12 keyboard to full keyboard

6. Touchscreen or navigation: keyboard or navigation mode changes,

If the keyboardhidden option is missing, the destruction of the activity cannot be prevented, and in the onconfigurationchanged event mentioned later, only the event that the vertical screen changes to the horizontal screen can be captured, and the horizontal screen changes to the vertical screen cannot be captured.

Android: onconfigurationchanged actually corresponds to the onconfigurationchanged () method in the activity. At androidmanifest The meaning of adding the above code to XML means that when changing the screen direction, popping up the software disk and hiding the soft keyboard, oncreate() method is no longer executed, but onconfigurationchanged() is directly executed. If this code is not declared, the oncreate () method will be executed once according to the life cycle of the activity, and the oncreate () method will usually do some initialization before displaying. Therefore, if the oncreate () method is executed for operations such as changing the screen direction, it may cause repeated initialization, which is inevitable to reduce the program efficiency, and it is more likely to cause data loss due to repeated initialization. This needs to be avoided.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>