• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps getRequestedOrientation() returns -1 onCreate

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?
 
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).
 
Upvote 0
what class are getRotation() declared from? Is it directly off the Activity? Or some other class?

Nil said:
the way you should be getting the screen orientation is through the Display class's getRotation or getOrientation methods.
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).
 
Upvote 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
 
Upvote 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?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones