Detailed Android Activity runtime screen direction and display mode _android

Source: Internet
Author: User

Now our mobile phones are generally built-in directional sensors, mobile phone screen will be based on the location of the automatic vertical and horizontal screen switching (if the screen is not locked). But sometimes our apps run only in a horizontal or vertical state, and we need to lock the screen orientation of the program's activity when it runs. There is a mobile phone when we watch the video, random to the screen switch, but the playback progress will not be with the screen to start from the beginning to play, in order to achieve this function, we need to change the activity in the current data to save.

Now based on the above two requirements, individuals to propose the following solutions:

First, lock activity run time screen direction, the following figure (Demo lock horizontal screen):

There are two ways to lock the orientation of the Activity Runtime screen:

(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 accomplish this task (portrait to keep the vertical screen, landscape to keep the horizontal screen)

<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
 package= " Com.example.administrator.day18 ">
 <application
 android:allowbackup=" true "
 android:icon=" @ Mipmap/ic_launcher "
 android:label=" @string/app_name "
 android:supportsrtl=" true "
 android:theme=" @ Style/apptheme ">
 <activity android:name=". Main2activity "
 ///Add Screenorientation Property (portrait to keep vertical screen, landscape to keep horizontal screen)
  android:screenorientation=" Landscape ">
  <intent-filter>
  <action android:name=" Android.intent.action.MAIN "/>
  <category android:name= "Android.intent.category.LAUNCHER"/>
  </intent-filter>
 </ activity>
 </application>
</manifest>

(2) implemented through Java code:

Import Android.content.pm.ActivityInfo;
Import Android.os.Bundle;
Import android.support.annotation.Nullable;
Import android.support.v7.app.AppCompatActivity;
/**
 * Created by Panchengjia on 2016/12/9.
 *
/public class Test extends Appcompatactivity {
 @Override
 protected void onCreate (@Nullable Bundle Savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main2);
 Add the Setrequestedorientation method to achieve a locked horizontal screen (portrait to keep the vertical screen, landscape to keep the horizontal screen)
 setrequestedorientation ( Activityinfo.screen_orientation_landscape);
 }

The first requirement is now implemented by modifying the configuration file as well as Java code, but it should be noted that when directional locking is set in both the configuration file and in the Java file, the system is set to the configuration file (which can be understood to have higher profile precedence than Java code )

Ii. preservation of data in the course of activity conversion

Here we show the activity conversion direction without saving data:

In the demo, we determine whether the data is saved by recording the click Count (Toast) of the button.

We found that when the screen (acticity) was turned from a horizontal screen to a vertical screen, the data was not saved and continued to count the number of hits from 0.

When we set the save data, the demo is as follows:

After saving the data, click on the toast will continue the vertical state of the data update, instead of starting from scratch.

For data preservation We also have two methods of implementing 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 (this writes the entire lifecycle of the activity and prints log for profiling performance):

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Toast;
 /** * Created by Panchengjia on 2016/12/9.  */public class Main2activity extends appcompatactivity {int num=1;//initial hits are 1 @Override protected void OnCreate (Bundle
 Savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main2);
 if (savedinstancestate!=null) {num=savedinstancestate.getint ("data");
 } log.i ("Tag", "onCreate");
 //Set Click event Toast Record Data public void Show (View v) {Toast.maketext (this, num++ + "", Toast.length_short). Show ();
 } @Override protected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate);
 Outstate.putint ("Data", num);
 LOG.I ("Tag", "onsaveinstancestate");
 } @Override protected void OnStart () {Super.onstart ();
 LOG.I ("Tag", "OnStart");
 } @Override protected void Onresume () {super.onresume ();
 LOG.I ("Tag", "Onresume"); } @Override ProteCTED void Onrestart () {Super.onrestart ();
 LOG.I ("Tag", "Onrestart");
 } @Override protected void OnPause () {super.onpause ();
 LOG.I ("Tag", "OnPause");
 } @Override protected void OnStop () {super.onstop ();
 LOG.I ("Tag", "onStop");
 } @Override protected void OnDestroy () {Super.ondestroy ();
 LOG.I ("Tag", "OnDestroy"); }
}

The core code for functional implementation is the protected void Onsaveinstancestate (Bundle outstate) method. The log of the activity before and after rotation is as follows:

When the screen (acticity) rotation is known from the log, the previous activity is destroyed and the rotated activity is rebuilt.

(2) Modify the Androidmanifest.xml configuration file

Add the Configchanges attribute to the Androidmanifest.xml, as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 package=" Com.example.administrator.day18 >
 <application
 android:allowbackup= "true"
 android:icon= "@mipmap/ic_launcher"
 android:label= "@string/app_name"
 android:supportsrtl= "true"
 android:theme= "@style/apptheme" >
 <activity android:name= ". Main2activity
 //Add Configchanges Property  android:configchanges= "Orientation|keyboard|screensize" >
  <intent-filter>
  <action android:name= "Android.intent.action.MAIN"/>
  <category android: Name= "Android.intent.category.LAUNCHER"/>
  </intent-filter>
 </activity>
 <!-- <activity android:name= ". Main2activity "></activity>-->
 </application>
</manifest>

Add the corresponding onconfigurationchanged () method in Java to log print analysis, the method itself has no practical significance in the realization of the function:

@Override public
 void onconfigurationchanged (Configuration newconfig) {
 super.onconfigurationchanged ( Newconfig);
 LOG.I ("Tag", "onconfigurationchanged");
 

Before and after the screen rotation log is as follows (demo two times rotation):

when you know the screen (acticity) rotation from the log, using the second method to save the data does not destroy the activity, creating the activity without repeated destruction to optimize the memory performance. It is recommended that you use this method

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.