The horizontal and vertical screen switching of activity in Android

What needs to be explained before the explanation is

Sets the orientation of the screen

brief introduction

Official website document address

First kind

We can set the direction of activity in the Android manifest file

In this way, the activity will not be re created when switching between horizontal and vertical screens

Second

Android:android:configChanges

If we do not configure the configuration, the activity will automatically process it when the configuration changes. On the contrary, if we configure the corresponding configuration, when the new configuration changes, we will call back the onconfigurationchanged () method of the activity.

Let's take a look at some common ones that are worth introducing. For others that are not commonly used, hi please refer to the documents yourself. Official website address:

Android: configchanges common configuration

After Android 3.2, if we do the following configuration, the horizontal and vertical screen will not recreate the activity, but will call the onconfigurationchanged () method

Summary

When we make the above configuration,

Therefore, we can usually deal with it accordingly

What if we set Android: configchanges = "keyboardhidden | orientation | screensize" and Android: screenorientation = "portal" at the same time?

If we turn on the automatic rotation screen of the system and rotate the screen, the system will not change and the onconfigurationchanged method of activity will not be called.

When we manually call the setrequestedorientation () method to change the direction of the screen, we will still call the onconfigurationchanged method

extend

Set full screen mode

In actual projects, we usually fix the screen direction of our application and only deal with some specific activities that need to switch the screen. How can we uniformly set the screen direction?

The first method is to copy and post, and add the following tags to each activity tag in the androidmanifest list file.

In the second method, I set Android: screenorientation directly in apptheme at the beginning, but it didn't work. At the beginning, I was also puzzled. Later, I checked the official website documents and described them below to solve the problem

Specify the orientation an activity should be run in. If not specified,it will run in the current preferred orientation of the screen. This attribute is supported by the <activity> element.

That is, this method is not desirable

The third method is to set it dynamically in baseactivity

At the same time, the main purpose of listing these three methods is not to tell you that these methods can uniformly set the direction of the screen. The most important thing is to tell you an idea. It can usually be set in XML files or in Java code.

Use the loading mechanism of the system to automatically help us load the corresponding layout

If you add layout land (horizontal layout folder) and layout port (vertical layout folder) to the resource directory res, restart the horizontal and vertical screen switching of activity mode,

Some people may have such a question. When we set the direction of the activity to vertical screen or horizontal screen, rotating the screen will not recall each life cycle of the activity. How do we detect it?

In fact, it is very simple, that is, we use our sensors, and then do corresponding processing according to the direction of rotation

Save the interactive state of the activity when the device rotates

Let's take a look at the life cycle of the activity. We know that if we do not configure the direction of the activity or the Android: configchang property of the activity, the activity will be re created every time the screen is rotated. How do we save our current state.

In fact, we can consider saving our corresponding data in onpause() or onstop(), and then judge whether savedinstancestate caches our data in oncreate() method. As for whether to save data in onpause () or onstop (), it depends on the specific requirements analysis. Onpause() will call back when the interface loses focus, and onstop() method will call back when the interface is completely invisible.

Demo download address: screendemo_ jb51.rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>