getRequestedOrientation() returns -1 onCreate


Last Updated:

  1. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    At the start up of my app (in onCreate() ), I need to know whether the G1 is flipped open or closed, but getRequestedOrientation() always returns -1 at the startup. Once the program is running and I flip it open and closed, getRequestedOrientation in the onConfigChanged() worked, the screen flips accordingly, but that function doesn't seem to work in onCreate(). Is there another function I should use to get the orientation of the G1 or whether it is flipped open or closed?
     

    Advertisement
  2. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    Is it possible to get the screen orientation at application startup?
     
  3. Nil

    Nil Member

    Joined:
    Jul 26, 2010
    Messages:
    15
    Likes Received:
    3
    Note: For anyone reading this and who has a similar problem, the answer is in response #8.

    Did you request a specific screen orientation? 'Cause the way you're doing it, you're only going to get the screen orientation your activity requested, not the current orientation (at least that's what I'm assuming, based on the docs). If not, and you just want the orientation the screen is currently using, the way you should be getting the screen orientation is through the Display class's getRotation or getOrientation methods.

    Bear in mind, the latter is deprecated in 2.2, and it's recommended you use getRotation. However, getRotation is only available in 2.2, so it depends on which API version you're targeting - you could also check, using reflection, to see if getRotation is implemented and use that when it is.

    That said, I don't know if you can get the orientation in onCreate. I assume you can, but I haven't tried this myself (since I haven't needed to know the orientation at that point).
     
  4. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    what class are getRotation() declared from? Is it directly off the Activity? Or some other class?
     
  5. Nil

    Nil Member

    Joined:
    Jul 26, 2010
    Messages:
    15
    Likes Received:
    3
    Emphasis added. This isn't explained in the docs, so I'll explain it here: you can get an instance of the Display class using the WindowManager class (how you get that is explained in the docs, so I won't reiterate it here).
     
  6. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    Display disp = (Display)getSystemService(Context.WINDOW_SERVICE);
    int orientation = disp.getOrientation();

    That function broke in onCreate()... The getSystemService() function broke. It gave me a RuntimeException. Should I not get a service in onCreate()?

    Edit:
    Woops, I read it too fast and realized I did something silly... Give me a minute. heh
     
  7. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
    Display disp = wm.getDefaultDisplay();
    int orientation = disp.getOrientation();

    My 'orientation' comes out to be 0, which is an undefined orientation, at least in the enums of Configuration. I'm guessing getting the orientation at onCreate(), it doesn't know what the orientation is, correct? I should do that at onResume() or something?
     
  8. Nil

    Nil Member

    Joined:
    Jul 26, 2010
    Messages:
    15
    Likes Received:
    3
    After some brief Googling (very brief), it appears you can also get the orientation through the Configuration class's orientation member (and, according to the comment I'm basing this off, that's supposed to be the preferred way of determining the orientation), and an instance of that class can be had via Resources.getConfiguration().
     
    Leolicos likes this.
  9. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    Great!! That worked. :) Thanks
     

Share This Page

Loading...