Reputation: 111
When using setRequestedOrientation() in onCreate() for a few seconds I get the phone orientation not the app orientation.
For example: orientation is set to be landscape (using setRequestedOrientation - in onCreate) if the phone is in portrait my app is in portrait for a few seconds than the app goes in landscape.
Using: setRequestedOrientation in onCreate() Added: orientation as configChanges in AndroidManifest
Notes: This is happening only in Android Pie 9.0
Upvotes: 2
Views: 594
Reputation: 1788
The only way to get around this is to specify the screen orientation in the Manifest. If you need to be able to specify the screen orientation programatically, then there's no good way of achieving it without the initial wrong orientation being displayed.
This is because by the time onCreate(...)
is called, most of the Activity is already initialised (with either default values, or values from XML). When you call setRequestedOrientation()
this acts as a change in the activity's original configuration, and will be processed whenever the main thread will finish its current work and get around to execute the command.
You can experience a similar delay when for instance you set the colour of the status bar from a custom theme. If you apply the theme on the activity in the manifest, the status bar will color itself as soon as the activity opens. If however you apply the theme in onCreate()
, then the previous colour of the status will be visible for a short period of time, until the colour mentioned in the theme is applied.
One last thing to note...in my experience the theme example & possibly even this orientation case are somewhat also dependent on the device and maybe Android version. The settings in the manifest are always applied to the activity before it shows, but some devices might apply the things requested in onCreate
so fast that you won't notice it.
Upvotes: 2
Reputation: 41
In Manifest,
<activity
android:name="Your Activity Name"
android:screenOrientation="landscape" />
In Activity onCreate,
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Upvotes: 1