Snake
Snake

Reputation: 14648

Set activity to specific orientation before it start

Is there a way I can start activity in a specific orientation (programmatically) without having it to be recreated again? Right now if I call setRequestedOrientation from the the OnCreate and the orientation is different than the current then the activity is destoryed and restarted again.

Thank you

Upvotes: 8

Views: 2016

Answers (3)

user3098756
user3098756

Reputation: 340

For me solution was to lock orientation of Activity

android:screenOrientation="locked"

, and now it depends only on setRequestedOrientation():

   <activity android:name=".SomeActivity"
      android:label="@string/app_name"
      android:screenOrientation="locked">

No restarting anymore.

Upvotes: 6

UserJava
UserJava

Reputation: 98

  1. If you don't want activity to restart, add configChanges for the activity in AndroidManifest.xml

    <activity
        android:name="com.myexample.SplashScreen"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name" >
    

    and then call setRequestedOrientation to set orientation programtically.

  2. But if you want to lock the activity to 1 orientation. It is good to set it in Manifest by adding android:screenOrientation="portrait"

Upvotes: 1

Ranjith
Ranjith

Reputation: 510

You can set your orientation in Manifest file

       <activity android:name=".SomeActivity"
          android:label="@string/app_name"
          android:screenOrientation="portrait">

Upvotes: 1

Related Questions