Android 8.1 orientation issues in Fragment

169 views Asked by At

I am trying to set one particular fragment orientation as Landscape mode. I have added setRetainInstance(true); in onCreate method.

I have added getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); in onCreateView method.

It is working fine with 7.1 and lesser versions. But it is not working with 8.1.

I have also tried the following links. Link1 Link2 and many other solutions. But those solutions were applicable for activity. In my case, I am using a fragment. Could anyone help me to solve this issue?

I have the following code in my manifest.

 <activity
        android:name=".activity.HomeScreen.view.HomeActivity"
        android:label="@string/title_activity_home"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" /> 
           
2

There are 2 answers

3
Legion On

In the manifest look the Activity who contain the fragment and check if you have the following string:

 android:configChanges="orientation|keyboardHidden|screenSize"

Remove it. It prevent to override landscape layout

1
Padmini S On

Above code given by you in onCreateView method is working fine with fragment in both Android 8 oreo and android 9 pie.

If you haven't added onPause() in fragment please add

 @Override
 public void onPause() {
  super.onPause();
  getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
  }

It's not required to set setRetainInstance(true); in onCreate() method for the above versions.

Check if it's a particular phone issue and please check if auto rotate is disabled in that phone.