2

I'd like to have all activities in portrait orientation.

So I have tried both

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

in onCreate(), onPause() or onResume() but the screen will flip to landscape and back to portrait in case of I rotate device in landscape from the beginning before opening the app.

But if I remove setRequestedOrientation method and just add android:screenOrientation="portrait" in AndroidManifest.xml only then it works (but let's imagine if I have 30 activities so it's better to have in BaseActivity instead)

How to reproduce

  1. BaseActivity has method in onCreate(), onResume(), onPause() setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  2. Rotate device in landscape orientation.

  3. Open app, Activity A is flipping from landscape to portrait.
  4. Start Activity B from Activity A, Activity B is flipping from landscape to portrait.

Note I have checked already Android 8.1 screen orientation issue: flipping to landscape a portrait screen

Activity rotating itself and back to normal in android 8.1

but it didn't work (it works only if orientation is defined in AndroidManifest)

If someone knows how to fix please share the solution

Thank you so much.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Why you want it in activity itself ?. All the activities are not portrait?. – Sunil Sunny Apr 24 '18 at 05:53
  • I want all activities to be portrait, right now If I rotate the device then the activity will be landscape that it's not my expected according to the requirement. – Shoryuken Apr 24 '18 at 06:58
  • You said if you are giving portrait in manifest then it's working as expected. Then why don't you do that?. Why are you still trying to do it in each activity?. – Sunil Sunny Apr 24 '18 at 07:02
  • I have 10+ activities that mean I have to define portrait in each activity in AndroidManifest so I think it's better if I can do it grammatically in BaseActivity class so all activities can extend this base class. – Shoryuken Apr 24 '18 at 07:05
  • ok understood. But if nothing is working then you may have to do it in `manifest` itself. It would been a lot easier if the application tag had orientation. You can also check `onConfigurationChanged`. – Sunil Sunny Apr 24 '18 at 07:15
  • Also is `setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);` the first line in onCreate.? – Sunil Sunny Apr 24 '18 at 07:22
  • Yes I did it already as I mentioned in the description, I also tried before and after `onCreate` but it didn't work on Pixel2 OS8.1.0 (OS7 and below works fine) – Shoryuken Apr 24 '18 at 07:29
  • Have you find a solution? – cylon May 07 '19 at 20:39
  • @cylon Not yet at that time, I had to add `android:screenOrientation="portrait"` in `AndroidManifest.xml` – Shoryuken May 16 '19 at 10:20

0 Answers0