Search code examples
androidandroid-orientation

Locking orientation to portrait force close in my phone


I want my app to lock into portrait mode. For this I used this code :

<activity android:name="MyActivity"
                  android:label="@string/app_name" android:screenOrientation="portrait"
                  android:configChanges="orientation|keyboardHidden"
                  >

and in MyActivity class :

public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

This code work correctly in emulator, but when I install and run in my phone, the app is force closed. How can I solve this problem?


Solution

  • Yo already use in manifest then there is no need to use by pragmatically. So remove the onConfigurationChanged method from your code.