-2

When the page loads, first it loads in LANDSCAPE_MODE and then after a few seconds the orientation changes to PORTRAIT_MODE the value

protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate();

    setContentView(R.layout.layout);

    if(orientation.equals("true")) 
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    else
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}
Shainu Thomas
  • 360
  • 3
  • 12
  • than what is the issue – Goku Nov 17 '17 at 07:16
  • what problem are you facing? read http://techblogon.com/android-screen-orientation-change-rotation-example/ – IntelliJ Amiya Nov 17 '17 at 07:17
  • Since your code is in **onCreate(Bundle savedInstanceState)** it doesn't wait for server's response and runs else condition OR **default condition mentioned in Manifest file** . you need to wait for server's response before proceeding with this. – Sara Tirmizi Nov 17 '17 at 07:18
  • 1
    check API work on PREVIOUS activity or APPLICATION class. – IntelliJ Amiya Nov 17 '17 at 07:19
  • I already have the value from server in my POJO class. – Shainu Thomas Nov 17 '17 at 07:20
  • is it because i'm setting the Orientation after setContentView? – Shainu Thomas Nov 17 '17 at 07:22
  • @ShainuThomas what problem you are faciing than – Goku Nov 17 '17 at 07:22
  • The problem is when the activity opens, first the activity loads in default mode and then after few seconds the orientation changes to what is expected. – Shainu Thomas Nov 17 '17 at 07:24
  • 3
    @UmairI I think you need to reconsider the Activity lifecycle again becuase onCreate() is called before onStart() https://developer.android.com/guide/components/activities/activity-lifecycle.html – Burhanuddin Rashid Nov 17 '17 at 07:25
  • @BurhanuddinRashid Oh yes I forgot that my bad. :) – Umair Nov 17 '17 at 07:27
  • 1
    @Shainu set `android:screenOrientation` to **user** and update your preference of orientation before jumping to activity: **user = The user's current preferred orientation.** For more info: https://developer.android.com/guide/topics/manifest/activity-element.html#screen – Sara Tirmizi Nov 17 '17 at 07:28
  • 1
    Thanks @SaraTirmizi . This worked for me. :). – Shainu Thomas Nov 17 '17 at 07:38
  • @ShainuThomas My pleasure, I am posting it as an answer and kindly mark that checked so that others can also get benefit from it :) – Sara Tirmizi Nov 17 '17 at 07:41
  • Why do you have to do that manually? Android natively handles screen rotations by itself! – Phantômaxx Nov 17 '17 at 08:44
  • I am aware of that, pertaining to my requirement, the screen should be in landscape or portrait mode irrespective of the device type. – Shainu Thomas Nov 17 '17 at 09:21
  • Orientation is irrespective of device type, natively. – Phantômaxx Nov 17 '17 at 10:09
  • also in my case , once a mode is decided , it should not be changed. for example, if the mode that is expected from the server is "landscape" , the orientation should always be landscape, the user should not be able to change the orientation and the problem in my case is that , every time the activity loads, the orientation first gets into the default mode and then after lets say 1 second, the mode changes to "landscape". – Shainu Thomas Nov 17 '17 at 10:15

3 Answers3

0

By default whatever the devices is in the current orientation is it will opens the activity in that mode.There is no specific way to handle this

As your changing the orientation programmatically in the activity which will be execute only after activity onCreate() method

So you will always face a glitch every time.I have face similar kind of orientation issue here Splash Screen Image for landscape and Portrait using Theme

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
0

@Shainu set

 android:screenOrientation = "user"

and

update your preference of orientation before jumping to activity. The user's current preferred orientation.

For more info: https://developer.android.com/guide/topics/manifest/activity-element.html

Sara Tirmizi
  • 417
  • 4
  • 10
0

The Best solution for me was to handle screen orientation before calling super.onCreate();

Shainu Thomas
  • 360
  • 3
  • 12