Android: How to stick to the same orientation the activity started with

advertisements

I know how to lock an activity in a specific orientation (in AndroidManifest.xml):

android:screenOrientation="landscape|portrait"

I know how to lock to a specific orientation programmatically:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

But, how do I lock the activity to the orientation in which started at? e.g., if it started portrait, it should stick to that.

Thanks!


Use getResources().getConfiguration().orientation when your app starts, then programmatically set the orientation like you do above. The method will return either ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT