Now our mobile phone is generally built with a directional sensor, the phone screen will be automatically based on the location of the vertical and horizontal screen switch (provided that the screen orientation is not locked). But sometimes our app can only run on a horizontal screen or a vertical screen, and we need to lock the screen orientation of the program's activity runtime. Also is when we use the mobile phone to watch the video, random to the screen switch, but the progress of the screen will not be played from the beginning, in order to achieve this function, we need to save the current data when the activity is converted.
Now according to the above two requirements, the individual proposed the following solutions:
One, lock the Activity runtime screen orientation, such as (Demo lock horizontal screen):
We can lock the orientation of the Activity Runtime screen in 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 property of the <activity> node can complete the task (portrait to keep the vertical screen, landscape to keep the horizontal screen)
1<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"2 Package= "Com.example.administrator.day18" >3<Application4Android:allowbackup= "true"5android:icon= "@mipmap/ic_launcher"6Android:label= "@string/app_name"7Android:supportsrtl= "true"8Android:theme= "@style/apptheme" >9<activity android:name= ". Main2activity "Ten //Add the Screenorientation property (portrait to keep the vertical screen, landscape to keep the horizontal screen) Oneandroid:screenorientation= "Landscape" > A<intent-filter> -<action android:name= "Android.intent.action.MAIN"/> - the<category android:name= "Android.intent.category.LAUNCHER"/> -</intent-filter> -</activity> -</application> +</manifest>
(2) Implemented by Java code:
1 ImportAndroid.content.pm.ActivityInfo;2 ImportAndroid.os.Bundle;3 Importandroid.support.annotation.Nullable;4 Importandroid.support.v7.app.AppCompatActivity;5 /**6 * Created by Panchengjia on 2016/12/9.7 */8 Public classTestextendsappcompatactivity {9 @OverrideTen protected voidonCreate (@Nullable Bundle savedinstancestate) { One Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main2); - //Add the Setrequestedorientation method to implement a locked horizontal screen (portrait to keep the vertical screen, landscape to keep the horizontal screen) - setrequestedorientation (activityinfo.screen_orientation_landscape); the } -}
Now the first requirement is implemented by modifying the configuration file as well as the Java code, but it is important to note that when the orientation lock is set in both the configuration file and the Java file, the system is set to the configuration file (which can be understood as the precedence of the configuration file above the Java code)
Second, the activity conversion direction when the data preservation
Below we show the situation where the data is not saved when the activity is in the direction of conversion:
In the demo, we determine whether the data is saved by recording the number of clicks (toast) of the button
We found that when the screen (acticity) was converted from a horizontal screen to a vertical screen, the data was not saved and continued counting the number of hits from 0 onwards.
When we set up to save the data, the demo is as follows:
After saving the data, clicking on the toast will continue to update the data in the vertical state, instead of starting from scratch.
We also have two methods for data preservation, namely the use of the onsaveinstancestate () method in Java code and the properties.
Below we analyze the implementation of these two methods in detail:
(1) onsaveinstancestate detailed
The implementation code is as follows (the entire life cycle of the activity is written, and the log is printed to analyze performance):
1 Importandroid.support.v7.app.AppCompatActivity;2 ImportAndroid.os.Bundle;3 ImportAndroid.util.Log;4 ImportAndroid.view.View;5 ImportAndroid.widget.Toast;6 /**7 * Created by Panchengjia on 2016/12/9.8 */9 Public classMain2activityextendsappcompatactivity {Ten intNum=1;//initial hit count is 1 One @Override A protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main2); the if(savedinstancestate!=NULL){ -Num=savedinstancestate.getint ("Data"); - } -LOG.I ("Tag", "OnCreate"); + } - //Set Click events Toast Record Data + Public voidShow (View v) { AToast.maketext ( This, num++ + "", Toast.length_short). Show (); at } - @Override - protected voidonsaveinstancestate (Bundle outstate) { - Super. Onsaveinstancestate (outstate); -Outstate.putint ("Data", num); -LOG.I ("Tag", "Onsaveinstancestate"); in } - @Override to protected voidOnStart () { + Super. OnStart (); -LOG.I ("Tag", "OnStart"); the } * @Override $ protected voidOnresume () {Panax Notoginseng Super. Onresume (); -LOG.I ("Tag", "Onresume"); the } + @Override A protected voidOnrestart () { the Super. Onrestart (); +LOG.I ("Tag", "Onrestart"); - } $ @Override $ protected voidOnPause () { - Super. OnPause (); -LOG.I ("Tag", "OnPause"); the } - @OverrideWuyi protected voidOnStop () { the Super. OnStop (); -LOG.I ("Tag", "OnStop"); Wu } - @Override About protected voidOnDestroy () { $ Super. OnDestroy (); -LOG.I ("Tag", "OnDestroy"); - } -}
The core code for the feature implementation is the protected void Onsaveinstancestate (Bundle outstate) method. The log before and after the activity rotation is as follows:
As you can tell from log that the screen (acticity) rotates, the previous activity is destroyed and the rotated activity is reconstructed.
(2) Modify the Androidmanifest.xml configuration file
Add the Configchanges attribute in Androidmanifest.xml, as follows:
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.example.administrator.day18" >4<Application5Android:allowbackup= "true"6android:icon= "@mipmap/ic_launcher"7Android:label= "@string/app_name"8Android:supportsrtl= "true"9Android:theme= "@style/apptheme" >Ten<activity android:name= ". Main2activity " One //Add Configchanges Property Aandroid:configchanges= "Orientation|keyboard|screensize" > -<intent-filter> -<action android:name= "Android.intent.action.MAIN"/> the -<category android:name= "Android.intent.category.LAUNCHER"/> -</intent-filter> -</activity> +<!--<activity Android:name= ". Main2activity "></activity>--> -</application> +</manifest>
Adding the corresponding onconfigurationchanged () method in Java is used for log printing analysis, which has no practical meaning in the implementation of the function:
1 @Override 2 Public void onconfigurationchanged (Configuration newconfig) {3 Super . onconfigurationchanged (newconfig); 4 LOG.I ("Tag", "onconfigurationchanged"); 5 }
The screen rotates before and after the log as follows (demo two rotations):
as you can tell from log that the screen (acticity) rotates,Saving data using the second method does not destroy activity, and it does not have to be destroyed repeatedly to create activity to optimize memory performance.
It is recommended to use this method
An explanation of the screen orientation and display mode of activity runtime in Android