0

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" /> 
           
aminography
  • 21,986
  • 13
  • 70
  • 74

2 Answers2

0

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

Legion
  • 760
  • 6
  • 23
  • I have the following code in my manifest. – Sridhar Venkatesan Sep 27 '19 at 05:40
  • Hi @Legion. I have updated my question also. I don't have configChanges in my manifest. Please let me know if you have any idea to solve this issue. – Sridhar Venkatesan Sep 27 '19 at 05:42
  • Not sure that can help you.... but can you try to [handle it by yoursealf instead](https://developer.android.com/guide/topics/resources/runtime-changes)? Seems odd, your code should work even in android 8.1 and higher :( – Legion Sep 27 '19 at 08:57
0

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.

Padmini S
  • 143
  • 1
  • 10