开发者

How to prevent change of orientation when device is tilted?

How can I prevent the activity from redraw开发者_开发知识库ing in the new orientation out when the device is tilted? I want the onCreate function to not execute a second time after the activity has been shown.


If you want to cope with tablet devices then you should use the nosensor value instead, e.g.

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

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

API docs for this are here:

The orientation is determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device. Except for this distinction, the system chooses the orientation using the same policy as for the "unspecified" setting.

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


you can fix the screen orientation. Add this to your activity tag in your manifest file:

android:screenOrientation = "portrait"


Add this code in your activity tag in AndroidManifest.xml

android:configChanges="keyboardHidden|orientation"

By adding this code your onCreate() will not be called when you change the phone mode to portrait or Landscape.


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 stuff for the AdMob:

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


Add this to your onCreate() based on your need and you are done.

requestWindowFeature(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

or

  requestWindowFeature(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

And still if you dont wish to change your code, add this in your manifest,

        <activity android:name=".activityname" android:label="Something"
        android:screenOrientation="portrait">
    </activity>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜