alex2k8 alex2k8 - 3 months ago 12
Android Question

How is application orientation (landscape or portrait) locked?

I have tried to freeze orientation using:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


Although the display stays in portrait orientation, the activity is still recreated. Any ideas how to solve this?

How can the orientation of the application be locked such that the activity is not recreated on orientation change?

Answer

First, don't use setRequestedOrientation() if you can avoid it. Use the android:screenOrientation attribute in your <activity> manifest element instead.

Second, you will also need android:configChanges="keyboardHidden|orientation" in your <activity> manifest element to prevent the destroy/recreate cycle.