Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set screen orientation before activity become visible

Reading carefully android documentation about setRequestedOrientation I found:

  • Change the desired orientation of this activity. If the activity is currently in the foreground or otherwise impacting the screen orientation, the screen will immediately be changed (possibly causing the activity to be restarted). Otherwise, this will be used the next time the activity is visible.

"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.

like image 624
Kostadin Avatar asked Dec 06 '25 07:12

Kostadin


2 Answers

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.

like image 138
user3098756 Avatar answered Dec 08 '25 21:12

user3098756


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.

like image 23
Gophermofur Avatar answered Dec 08 '25 20:12

Gophermofur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!