Explain the screen direction and display mode of activity runtime in Android

At present, our mobile phones generally have a built-in direction sensor, and the mobile phone screen will automatically switch between horizontal and vertical screens according to the location (provided that the screen direction is not locked). However, sometimes our application can only run in the horizontal or vertical screen state. At this time, we need to lock the screen direction when the program activity is running. In addition, when we watch video on our mobile phone, we can switch the horizontal and vertical screens at will, but the playback progress will not start from scratch with the screen conversion. In order to realize this function, we need to save the current data during activity conversion.

Now, according to the above two needs, I propose the following solutions:

1、 Lock the screen direction when the activity is running, as shown in the following figure (demonstrate locking the horizontal screen):

We can lock the orientation of the activity runtime screen in the following two ways:

(1) By modifying the androidmainfest.xml configuration file

Modify the configuration file under Android / APP / mainfests / androidmainfest.xml as follows:

The Android: screenorientation attribute of the < activity > node can complete this task (portrait is to keep the vertical screen and landscape is to keep the horizontal screen)

(2) Implemented by java code:

Now, the first requirement is realized by modifying the configuration file and Java code, but it should be noted that when direction locking is set in the configuration file and Java file at the same time, the system will take the configuration file setting as the standard (it can be understood that the priority of the configuration file is higher than that of Java code)

2、 Saving of data when activity changes direction

Below, we show the case where the activity does not save data when changing directions:

In the demonstration, we judge whether the data is saved by recording the number of clicks of the button (chieftain)

We found that when the screen (acticity) changes from horizontal screen to vertical screen, the data is not saved. Continue to count the clicks from 0.

After we set the save data, the demonstration is as follows:

After saving the data, click several times, and the toast will continue to update the data in the vertical screen state instead of starting from scratch.

For data saving, we also have two methods: using onsaveinstancestate() method in Java code and property.

Let's analyze the implementation of these two methods in detail:

(1) Onsaveinstancestate details

The implementation code is as follows (the entire life cycle of the activity is written here, and the log is printed for performance analysis):

The core code of the function implementation is the protected void onsaveinstancestate (bundle outstate) method. The log before and after activity rotation is as follows:

From the log, it can be seen that when the screen rotates, the previous activity will be destroyed, and then the rotated activity will be rebuilt.

(2) Modify the androidmanifest.xml configuration file

Add the configchanges attribute in androidmanifest.xml as follows:

Add the corresponding onconfigurationchanged () method in Java for log print analysis. The method itself has no practical significance in function implementation:

The log before and after screen rotation is as follows (demonstrate two rotations):

From the log, we can know that when the screen rotates, using the second method to save data will not destroy the activity, and there is no need to repeatedly destroy and establish the activity, so as to optimize the memory performance. This method is recommended

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