Android to judge the problem of the screen _android

Source: Internet
Author: User
Tags call back

Android on the screen to solve the problem should be two: first. Layout problems; Reload the problem.
1. Layout problem:
If you don't want the software to switch between the two screens, the easiest way to do this is to find the activity you specified in the project's Androidmanifest.xml plus Android: Screenorientation attribute, he has the following several parameters:
"Unspecified"
The default value is determined by the system to determine the direction of the display. The strategy is related to the device, so different devices have different display orientations.
"Landscape"
Horizontal screen display (width ratio is long)
"Portrait"
Vertical screen display (height is longer than width)
"User"
The user's current preferred orientation
"Behind"
In the same direction as the activity under the activity (in the activity stack)
"Sensor"
There are physical sensors to decide. If the user rotates the device, the screen will switch at any screen.
"Nosensor"
Ignores physical sensors so that they do not change as the user rotates the device (except for the "unspecified" setting).
It can also be set by Setrequestedorientation (Activityinfo.screen_orientation_landscape) in Java code.
If you want to allow the software to switch between the two screens, because the width of the screen will be converted, it may require different layouts. You can switch layouts in the following ways:

1 in the Res directory to establish Layout-land and Layout-port directory, the corresponding layout file unchanged, such as Main.xml. Layout-land is the horizontal screen of the Layout,layout-port is the layout of the vertical screen, the other without the tube, the simulator will automatically find.

2) by This.getresources (). GetConfiguration (). Orientation to determine whether the current is a horizontal or vertical screen and then load the corresponding XML layout file. Because when the screen changes to a horizontal screen, the system will call back the current Activity OnCreate method, you can put the following methods in your oncreate to check the current direction, and then let your setcontentview to load different layout XML.

Copy Code code as follows:

if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {
LOG.I ("info", "landscape");
}
else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait) {
LOG.I ("Info", "Portrait");
}

2. Re-loading the issue. If you do not need to reload, you can add the configuration android:configchanges= "orientation" to the Androidmanifest.xml and configure Android: Configchanges's role is as stated in the document: Specify one or more configuration changes this activity would handle itself. If not specified, the activity would be restarted if any of this configuration changes happen in the system. This is in the program. The activity will not repeat the call OnCreate () or even call Onpause.onresume. Only one onconfigurationchanged (Configuration newconfig) will be invoked.
–************* actually here I met two strange questions, that is
1.
If I only set the orientation in Android:configchanges, he will still reload, only set the orientation| Keyboardhidden it will only call one onconfigurationchanged (Configuration newconfig)

2. when the horizontal screen changes to the vertical screen, he will call two times onconfigurationchanged, and the vertical screen to the horizontal screen when he only called once onconfigurationchanged, it is very strange. If you know, please leave a message to discuss together *************–
If you need to reload, you do not need to make any changes. However, if you need to save the previous operation or data in the reload process, you need to save the previous data. It is then taken out of the OnCreate () of the activity. Of course, this cannot be set android:configchanges (), otherwise the OnCreate () method will not be invoked. So where can the data be kept? There are four kinds of storage methods in Android and you can also use Android to provide us with a onretainnonconfigurationinstance () method to temporarily save data.
Here is an example:
To save a temporary picture:

Copy Code code as follows:

@Override
Public Object onretainnonconfigurationinstance () {
Final loadedphoto[] list = new Loadedphoto[numberofphotos];
Keepphotos (list);
return list;
}

You can then invoke the temporary file in the OnCreate () function of the activity to determine whether the system needs to reload the temporary file in your code. Here's the code to load the temporary file in the OnCreate () function:
Copy Code code as follows:

private void Loadphotos () {
Final Object data = Getlastnonconfigurationinstance ();

The activity was starting for the ' the ' the ' the ' photos from Flickr
if (data = = NULL) {
Mtask = new Getphotolisttask (). Execute (mcurrentpage);
} else {
The activity is destroyed/created automatically, populate the grid
of photos with the images loaded by the previous activity
Final loadedphoto[] photos = (loadedphoto[]) data;
for (Loadedphoto Photo:photos) {
Addphoto (photo);
}
}
}

For the majority of the situation does not need to do as the operation, so you need to use this language carefully, after all, the best behavior does not apply to all situations, if the application of the bad will bring unnecessary trouble to the program.

If you want to completely prohibit flipping, you can set the Android:screenorientation property to Nosensor, so you can ignore the problem of gravity induction. But do not know why, in the simulator does not work, listen to others said in the real machine is correct, I do not have the real machine, and so have the real machine to try again.

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.