Orientation issue in Android 7.1.1 and above

406 Views Asked by At

When the app is launched in landscape mode it is opening in landscape mode and switching back to portrait evethough orientation is set to portrait programatically. I suppose app should stay in portrait mode no matter in which mode you launch when screen orientation is set to portrait programatically. I know if we set screen orientaion to portrait in Manifest will solve this but I need to set orientaion programatically. Let me know if anyone faced this issue or have any idea to fix this. This is happening only when app is launched. Next time when the screen is rotated it is working fine.

Below is the code I am using to lock orientaion to portrait,

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);
    }

this code working fine in below Android 7.1.1.

1

There are 1 best solutions below

0
On

The below code works for me on all devices:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} ... enter code here ...