How to prevent a change in orientation when tilting the device?

How can I prevent activity redrawing in a new orientation when the device is tilted? I want the onCreate function not to be executed a second time after showing the activity.

+4
source share
5 answers

You can correct the screen orientation. Add this to your activity tag in the manifest file:

 android:screenOrientation = "portrait" 
+8
source

If you want to handle tablet devices, you should use the nosensor value, for example, instead.

 <activity android:name=".MyActivity" android:screenOrientation="nosensor" ></activity> 

This will use the natural orientation of the device, which will be displayed on some devices (e.g. Xoom tablet).

API docs for this:

Orientation is determined without reference to the physical orientation sensor. The sensor is ignored, so the display will not rotate depending on how the user moves the device. Besides this difference, the system selects the orientation using the same policy as for the “unspecified” setting.

http://developer.android.com/guide/topics/manifest/activity-element.html#screen

+10
source

Add this code to your activity tag in AndroidManifest.xml

 android:configChanges="keyboardHidden|orientation" 

By adding this code, your onCreate() will not be called when the phone mode is changed to portrait or landscape.

+5
source

I had to add two things to the AndroidManifest file:

 <activity android:name="my_activity" ... android:configChanges="orientation" android:screenOrientation="portrait"> ... </activity> 

I also had to do similar things for AdMob:

 <activity android:name="com.google.ads.AdActivity" android:configChanges="orientation"/> 
+1
source

Add this to your onCreate () depending on your needs, and you're done.

 requestWindowFeature(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

or

  requestWindowFeature(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

And yet, if you don’t want to change your code, add this to your manifest,

  <activity android:name=".activityname" android:label="Something" android:screenOrientation="portrait"> </activity> 
+1
source

All Articles