Solve the horizontal and vertical screen switching of Android mobile phone screen

In Android, when the screen switches from horizontal to vertical, the life cycle of the activity is to reload (indicating that the current activity has been destroyed, but it is loaded again),

How can the current activity not be destroyed when the screen is switched from horizontal to vertical?

1. Set the configchanges attribute for the activity in androidmanifest.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 selects 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

2. Rewrite the onconfigurationchanged method in the corresponding activity:

The effects are as follows:

Log printing:

It can be seen from the log that the activity is not destroyed during screen vertical and horizontal switching. Of course, you can also run the project and make a breakpoint in the oncreate method. It is found that the oncreate method is only executed at the beginning of entry. It will not be executed during screen vertical and horizontal switching. Therefore, you can click the article in the onconfigurationchanged method!

Note: if the project does not require screen switching, it can be set to

1. Android: screenorientation = "portal" is always displayed in a vertical screen

2. Android: screenorientation = "landscape" is always displayed in a horizontal screen

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

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
分享
二维码
< <上一篇
下一篇>>