Chris Danson
Chris Danson

Reputation: 247

Ensure screen orientation is fixed to portrait and NEVER changes

I would like my Android app to instantiate it's home screen activity one time only. I am managing the back stack appropriately to achieve this but have just discovered an orientation issue when the app starts up.

Visually this orientation change only shows itself on the emulator. (probably runs too fast to be observed on a device).

Here's what happens :: -->

activity.onCreate()
activity.onDestroy()
activity.onCreate()

This sequence makes sense and is caused by the change in orientation. What does not make sense (to me) is that it happens at all because I have done the following to prevent an orientation change :: -->

AndroidManifest.xml contains

android:screenOrientation="portrait"

for all my activities and in the home screen activity onCreate() method, I'm calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

So why do I get an orientation change?

Upvotes: 1

Views: 4345

Answers (1)

Deepak Swami
Deepak Swami

Reputation: 3858

Add android:screenOrientation="portrait" in your manifest file where you declare your activity like this

<activity android:name=".yourActivity"
          ....
          android:screenOrientation="portrait"/>

if you want to do using java code try

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

before you setContentView for your activity in onCreate()

see here

Upvotes: 2

Related Questions