Precautions for force portrait or portrait screen in Android and usage of onConfigurationChanged

Source: Internet
Author: User

Next, although this is useless for rotation, it does not mean it is useful. If an Activity is not specifically declared in onReusume, or is not in AndroidManifest. xml is configured as a horizontal or vertical screen. The declaration period during rotation is onCreate ------ onStart --- onResume --- screen rotation ---- onPause (Focus-removing) ---- onStop (invisible) --- onDestory, and then repeat onCreate --- onStart ---- onResume again. To do this, you need to set the screen to landscape or landscape. There are three methods:

1. Set in AndroidManifest. xml,

Android: name = "org. yanzi. testrotateimageview. MainActivity"
Android: label = "@ string/app_name"
Android: screenOrientation = "portrait"
>

2. In onResume

If (getRequestedOrientation ()! = ActivityInfo. SCREEN_ORIENTATION_PORTRAIT ){
SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );
}
Super. onResume ();

This method is strongly not recommended. Who knows it? It will slow down when the app is started in different directions!

3. In onConfigurationChanged, the following conditions are required for onConfigurationChanged to be valid in the listener screen direction:

A. add permissions to AndroidManifest. xml:


B. The MiniSdkVersion and TargetSdkVersion attributes set in AndroidManifest. xml are greater than or equal to 13.

C. Add the following in AndroidManifest. xml Activity:

Android: configChanges = "keyboard | screenSize | orientation | layoutDirection"

After the above, we can detect the screen direction change event in onConfigurationChanged (). Once the direction is set in AndroidManifest. xml, this part will become invalid. NewConfig. orientation = 1 indicates a portrait screen, and 2 is a landscape screen. Determine this and then setRequestedOrientation () to set the landscape or landscape.

This onConfigurationChanged ()-related configuration has the biggest advantage of not performing onPause onStop when the Activity direction changes... If you want to use setRequestOrientation to set a horizontal or vertical screen, the negative effect will be the same as that of the 2nd method, which is slow to death.

In short, the direction is still in the configuration !!! If you want to rotate the image, you can use RotateImageView andRotating LayoutThis layout allows the child controls in it to rotate without affecting the lifecycle of the parent layout and Activity.

For more information, see link 1 link 2 link 3


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.