dop2000
dop2000

Reputation: 597

Lock orientation and avoid screen rotation when new activity opens

My app works in both portrait and landscape modes, so I have this configuration in my manifest for all activities:

android:screenOrientation="sensor"
android:configChanges="orientation|keyboardHidden|screenSize"

But I also want to give users an option to lock orientation in landscape mode. I added the following line to the onCreate method for all activities:

if (lockLandscape) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

It works, but when I'm holding my tablet vertically, the new activity first opens in portrait mode, and then in a moment rotates to landscape. The rotation back and force doesn't look very nice... Is there a way to fix this? Maybe disable the sensor somehow?

Upvotes: 2

Views: 2470

Answers (1)

Maurizio
Maurizio

Reputation: 4169

I ran into this same issue and now simply use the following code to lock the orientation before I show a new activity (and then unlock it once that activity is dismissed):

if (rotationEnabled)
{
    setRequestedOrientation(mSavedOrientation);
}
else
{
    mSavedOrientation = getRequestedOrientation();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}

Upvotes: 1

Related Questions