Details about the screen direction and display mode when the Android Activity is running. androidactivity

Source: Internet
Author: User

Details about the screen direction and display mode when the Android Activity is running. androidactivity

Currently, our mobile phones generally have built-in orientation sensors. The mobile phone screen will automatically switch between the horizontal and vertical Screens Based on the location (provided that the screen direction is not locked ). However, sometimes our applications can only run on a horizontal or vertical screen. At this time, we need to lock the screen direction when the program Activity is running. In addition, when we watch videos on our mobile phones, we can switch between the screen and the screen at will, but the playback progress will not start from the beginning as the screen changes. To achieve this function, we need to save the current data during Activity conversion.

Based on the above two requirements, I propose the following solutions:

1. Lock the screen direction when the Activity is running, such as (demonstration of locking the horizontal screen ):

We can lock the screen orientation when the Activity is running in the following two ways:

(1) modify the configuration file AndroidMainfest. xml

Modify the configuration file in Android/app/mainfests/AndroidMainfest. xml as follows:

<Activity> the android: screenOrientation attribute of the node can complete this task (portrait is used to keep the portrait screen, and landscape is used to keep the landscape screen)

 

1 <manifest xmlns: android = "http://schemas.android.com/apk/res/android" 2 package = "com. example. administrator. day18 "> 3 <application 4 android: allowBackup =" true "5 android: icon =" @ mipmap/ic_launcher "6 android: label =" @ string/app_name "7 android: supportsRtl = "true" 8 android: theme = "@ style/AppTheme"> 9 <activity android: name = ". main2Activity "10 // Add the screenOrientation attribute (portrait is used to keep the portrait screen, landscape is used to keep the landscape screen) 11 android: screenOrientation = "landscape"> 12 <intent-filter> 13 <action android: name = "android. intent. action. MAIN "/> 14 15 <category android: name =" android. intent. category. LAUNCHER "/> 16 </intent-filter> 17 </activity> 18 </application> 19 </manifest>

 

(2) implemented through java code:
1 import android. content. pm. activityInfo; 2 import android. OS. bundle; 3 import android. support. annotation. nullable; 4 import android. support. v7.app. appCompatActivity; 5/** 6 * Created by panchengjia on 2016/12/9. 7 */8 public class Test extends AppCompatActivity {9 @ Override10 protected void onCreate (@ Nullable Bundle savedInstanceState) {11 super. onCreate (savedInstanceState); 12 setContentView (R. layout. activity_main2); 13 // Add the setRequestedOrientation method to lock the landscape screen (portrait is to keep the portrait screen, landscape is to keep the landscape screen) 14 setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); 15} 16}

Currently, the first requirement is implemented by modifying the configuration file and java code. However, it should be noted that when the direction lock is set in the configuration file and java file at the same time, the system will follow the Configuration File Settings(It can be understood that the configuration file has a higher priority than the java code)

 

Ii. Data Storage during Activity Conversion

 

The following shows that no data is saved during Activity conversion:

In the demonstration, we can record the number of clicks (Tusi) of the button to determine whether the data is saved.

We found that when the screen (Acticity) is changed from landscape to landscape, the data is not saved and the number of clicks continues from 0.

After we set to save the data, the demo is as follows:

After saving the data, the clickthrough dashboard will continue to update the data in the portrait status, instead of starting from scratch.

There are two methods for data storage:OnSaveInstanceState ()Method and in attributes.

The following describes the implementation of these two methods:

 

(1) onSaveInstanceState

 

The implementation code is as follows (the entire lifecycle of the Activity is written here, and the log is printed for Performance Analysis ):

1 import android. support. v7.app. appCompatActivity; 2 import android. OS. bundle; 3 import android. util. log; 4 import android. view. view; 5 import android. widget. toast; 6/** 7 * Created by panchengjia on 2016/12/9. 8 */9 public class Main2Activity extends AppCompatActivity {10 int num = 1; // the initial number of clicks is 111 @ Override12 protected void onCreate (Bundle savedInstanceState) {13 super. onCreate (savedInstanceState ); 14 setContentView (R. layout. activity_main2); 15 if (savedInstanceState! = Null) {16 num = savedInstanceState. getInt ("data"); 17} 18 Log. I ("Tag", "onCreate"); 19} 20 // set the data record for clicking event Tuesday 21 public void show (View v) {22 Toast. makeText (this, num ++ "", Toast. LENGTH_SHORT ). show (); 23} 24 @ Override25 protected void onSaveInstanceState (Bundle outState) {26 super. onSaveInstanceState (outState); 27 outState. putInt ("data", num); 28 Log. I ("Tag", "onSaveInstanceState"); 29} 30 @ Override31 protected void onStart () {32 super. onStart (); 33 Log. I ("Tag", "onStart"); 34} 35 @ Override36 protected void onResume () {37 super. onResume (); 38 Log. I ("Tag", "onResume"); 39} 40 @ Override41 protected void onRestart () {42 super. onRestart (); 43 Log. I ("Tag", "onRestart"); 44} 45 @ Override46 protected void onPause () {47 super. onPause (); 48 Log. I ("Tag", "onPause"); 49} 50 @ Override51 protected void onStop () {52 super. onStop (); 53 Log. I ("Tag", "onStop"); 54} 55 @ Override56 protected void onDestroy () {57 super. onDestroy (); 58 Log. I ("Tag", "onDestroy"); 59} 60}

The core code for function implementation is the protected void onSaveInstanceState (Bundle outState) method. The logs before and after the Activity rotation are as follows:

The Log shows that when the screen (Acticity) is rotated, the previous Activity is destroyed and the rotated Activity is re-created.

(2) modify the AndroidManifest. xml configuration file

Add the configChanges attribute to 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 <application 5 android: allowBackup =" true "6 android: icon =" @ mipmap/ic_launcher "7 android: label =" @ string/app_name "8 android: supportsRtl = "true" 9 android: theme = "@ style/AppTheme"> 10 <activity android: name = ". main2Activity "11 // Add configChanges attribute 12 android: configChanges =" orientation | key Board | screenSize "> 13 <intent-filter> 14 <action android: name =" android. intent. action. MAIN "/> 15 16 <category android: name =" android. intent. category. LAUNCHER "/> 17 </intent-filter> 18 </activity> 19 <! -- <Activity android: name = ". Main2Activity"> </activity> --> 20 </application> 21 </manifest>

Add the corresponding onConfigurationChanged () method to java for Log printing and analysis. The method itself has no practical significance in function implementation:

 

1 @Override2 public void onConfigurationChanged(Configuration newConfig) {3    super.onConfigurationChanged(newConfig);4    Log.i("Tag","onConfigurationChanged");5 }

The Log before and after screen rotation is as follows (two rotations are shown ):

When the screen (Acticity) is rotated,Using the second method to save data does not destroy the Activity, and you do not need to destroy the Activity repeatedly to optimize the memory performance. This method is recommended.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.