Android 8.1 screen orientation issue: flipping to landscape a portrait screen

Just came across this problem in my own app.

The solution that works for me is as follows:

onCreate(){
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

onPause(){ 
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
  }
}

onResume(){
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
  }
}

The above code should go in the activity that is in landscape mode (i.e. the second activity, and the one you press the back button from)

I would like to point out that this solution was not my own, and I have taken it from the #20 post at the following link (which is also noted in the OP):

https://issuetracker.google.com/issues/69168442

I just thought it might be easier for people to access if they don't have to search another page for it.


This fixed the issue.

Override the onBackPressed() method of Landscape activity and set orientation to Portrait.

@Override
public void onBackPressed() {
    super.onBackPressed();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

If u have DialogTheme like theme=Theme.AppCompat.Light.Dialog in your manifesto, remove the set orientation tag for that Dialog activity from manifesto , It will take the Orientation from the previous activity and put setorientation tag for remaing activites

and for below Versions place this on oncreate

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  }

and set theme for application Theme.AppCompat.Light.DarkActionBar no need to add theme to activities