Reading carefully android documentation about setRequestedOrientation I found:
"Otherwise, this will be used the next time the activity is visible." Sound great - just set before activity become visible. OOOps. Where to call this? Actually before onCreate we don't have reference to activity. How to set screen orientation then?
Update: - My idea is to create activity directly with desired orientation. When user select orientation from settings - set screen orientation and all activities started after that - to know desired orientation /like if they use value from manifest/. If I know desired orientation - there is no reason to create activity, set orientation and create it again.
I was solving exactly the same situation, that Activity orientation is known and set by user in Settings and I want to start Activity in desired orientation.
For me solution was to lock orientation of Activity
android:screenOrientation="locked"
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="locked">
and onCreate() I call setRequestedOrientation():
No restarting anymore.
It seems prefectly reasonable to put it in the onCreate.
http://russenreaktor.wordpress.com/2010/01/03/solved-set-screen-orientation-programmatically/
If that doesn't work, you could try putting it in the onPostCreate(Bundle) overridden method.
protected void onPostCreate (Bundle savedInstanceState)
Since: API Level 1 Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called). Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run.
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With