onOrientationChange not triggering on Android?

Any ideas why an “onOrientationChange” I need in my game is not triggering in android.  

i.e. Runtime:addEventListener( “orientation”, onOrientationChange )

In build.settings I do have the following.  Is this not enough to trigger autoOrientation for Android?

settings = {         orientation =     {         default = "landscapeRight",         supported = { "landscapeLeft", "landscapeRight" },     },

I’m testing on the latest Nexus 7. 

Hi Greg,

Can you try using a “resize” event listener instead? Here’s the documentation for it (basically the same thing as orientation):

http://docs.coronalabs.com/api/event/resize/index.html

Best regards,

Brent

Greg,

You won’t get an orientation event when switching between landscapeLeft to landscapeRight or vice-versa.

You also won’t get an orientation event when switching between portrait and portraitUpsideDown.

It’s an Android OS limitation.  The OS itself does not provide an orientation event to the app for the above cases.  Google sees this as an optimization as most Android apps typically destroy and re-create the Activity window when orientation change occurs.  (Corona suppresses this as an optimization.)

The only time you’ll get an orientation event is in the following cases:

  • When switching between landscape to portrait and vice-versa.

  • When you have a fixed orientation app.  In which case, you’ll get the device orientation instead of the app’s orientation.  (See the “Graphics/Fishies” sample project for an example.)

Thanks guys. Perhaps putting these words in the API docs would help makes things extra clear.

Brent/Joshua - Unfortunately the  “resize” event hasn’t helped me.  It doesn’t trigger for my app on the simulator or on Android.  This seems to be supported by the document for “resize” when it says: 

"only occurs for apps that support both portrait and landscape orientations"

Follow up questions:

Q1 - What other ways could I get Corona to detect a change from one landscape orientation to the other landscape orientation? for an app that only supports landscape (not portrait) for Android.

Q2 - If Corona can not detect such a change for Android is there a way to have an if/then in the “build.settings” file based on device to constraint an Android device to only have one (not two) supported landscape orientations?  

Q3 - If no to Q1 & Q2 is the fallback to detect orientation each “enterFrame” event?   

UPDATE:  Actually some concerning is that I’ve just confirmed that on Android (on my new Nexus 7) that “system.orientation” is NOT changes when I tip the device upside down.  That is it stay on “landscapeRight”.  So would this be a Corona bug?   Only fix is going to be not allowing users to be able to tip/use the game upside down then no?  (i.e. in landscapeLeft)

As I’ve mentioned up above, there is no way to get an orientation event when switching between landscapeRight and landscapeLeft and vice-versa.  It’s not a Corona bug.  It’s an Android OS limitation.  You can only get orientation events on Android for the situations mentioned in my last post.  This is just how it is on Android.  What you want is simply not possible on Android.

Let me ask you this.  What are you trying to accomplish?

Because the Android OS already correctly flips your app when switching between landscape left and right.

Hi Joshua - For the release I’ve got underway I’ve taken out the support for landscapeLeft to get around the issue.  Would be nice to find a better work-around in the future hence I’ll try to give you more info below:

* Horizontal scrolling game with gravity, however to get the right dynamics of the player I’m managing his position/velocity myself

* As part of the players speed/direction change it depends on Tilt

* Was hoping to support both landscape directions, to be less annoying to users

* On IOS I have things working for the physics calculations by changing a factor to 1 or -1 depending on orientation (otherwise, in landscapeLeft mode the user would tilt backwards when you tilt the device to the right)

* On Android there seems no way to detect the orientation at all (when I have a landscape locked game) hence this is the issue

I thought it would be good to have both landscapes enabled for IOS, but only one for android, hence my question re build.settings.

Make sense?

I see.  I’m not sure how we can help you solve that problem.  While the accelerometer data may indicate that the device is flipped over, there is no way to know if the app window was actually flipped.  This is especially a problem for devices that do not support flipping to landscapeLeft, making it wrong to assume that the upside down accelerometer values are for a flipped app.

And I’m not jerking you around on this.  The Android OS gives us no clue at the native development side (Java or C/C++) that the app has been flipped.  We’re left in the dark just as much as you are.

I think hard coding your app to be “landscapeRight” is the easiest solution.  That said, the interesting consequence of this is that once your app becomes a “fixed orientation” app, meaning it only supports one orientation, Corona will turn on a sensor and provide an orientation event for all 4 orientations of the device.  At this point, the orientation event does not represent the orientation of your app window, but the orientation of the device.  We’re effectively bypassing Android’s flawed orientation event system and providing our own.  This allows Corona made apps to handle app rotation for themselves.  This also means you can rotate your entire scene and handle this yourself just the way you want.  It’s more work, but it’s the only other solution.

Oh.  I was not aware of the option you mention in your last para - sounds like a work around to still provide the same functionality for IOS without having to have if/then’s in the build.settings then?   

So re “Corona will turn on a sensor and provide an orientation event for all 4 orientations of the device” - then I don’t need to do anything differently in my app here for IOS for this to work?  Still use the orientation change event?

Right.  iOS will provide you all 4 orientations regardless of what orientations your app supports.  The difference is that Android will not automatically flip your app.  But that said, some Corona developers like handling app orientation themselves which is why we provide this option if your app has a fixed orientation.  A good example of this is the camera app on iOS and Android.  Camera apps typically have fixed orientations but rotate their buttons/controls when you rotate the device.

Hi Joshua - in fact I’m not too keen re “you can rotate your entire scene and handle this yourself” and having to go through this.  

Is it possible to put if/then statements in a build.settings file to allow 2 landscape orientations for IOS, but only 1 for Android?

It’s not possible to use an “if” condition.  There is no way for the “build.settings” to know if it is building for Android or iOS.

You’ll have to change your build settings when building for Android.

Hi Greg,

Can you try using a “resize” event listener instead? Here’s the documentation for it (basically the same thing as orientation):

http://docs.coronalabs.com/api/event/resize/index.html

Best regards,

Brent

Greg,

You won’t get an orientation event when switching between landscapeLeft to landscapeRight or vice-versa.

You also won’t get an orientation event when switching between portrait and portraitUpsideDown.

It’s an Android OS limitation.  The OS itself does not provide an orientation event to the app for the above cases.  Google sees this as an optimization as most Android apps typically destroy and re-create the Activity window when orientation change occurs.  (Corona suppresses this as an optimization.)

The only time you’ll get an orientation event is in the following cases:

  • When switching between landscape to portrait and vice-versa.

  • When you have a fixed orientation app.  In which case, you’ll get the device orientation instead of the app’s orientation.  (See the “Graphics/Fishies” sample project for an example.)

Thanks guys. Perhaps putting these words in the API docs would help makes things extra clear.

Brent/Joshua - Unfortunately the  “resize” event hasn’t helped me.  It doesn’t trigger for my app on the simulator or on Android.  This seems to be supported by the document for “resize” when it says: 

"only occurs for apps that support both portrait and landscape orientations"

Follow up questions:

Q1 - What other ways could I get Corona to detect a change from one landscape orientation to the other landscape orientation? for an app that only supports landscape (not portrait) for Android.

Q2 - If Corona can not detect such a change for Android is there a way to have an if/then in the “build.settings” file based on device to constraint an Android device to only have one (not two) supported landscape orientations?  

Q3 - If no to Q1 & Q2 is the fallback to detect orientation each “enterFrame” event?   

UPDATE:  Actually some concerning is that I’ve just confirmed that on Android (on my new Nexus 7) that “system.orientation” is NOT changes when I tip the device upside down.  That is it stay on “landscapeRight”.  So would this be a Corona bug?   Only fix is going to be not allowing users to be able to tip/use the game upside down then no?  (i.e. in landscapeLeft)

As I’ve mentioned up above, there is no way to get an orientation event when switching between landscapeRight and landscapeLeft and vice-versa.  It’s not a Corona bug.  It’s an Android OS limitation.  You can only get orientation events on Android for the situations mentioned in my last post.  This is just how it is on Android.  What you want is simply not possible on Android.

Let me ask you this.  What are you trying to accomplish?

Because the Android OS already correctly flips your app when switching between landscape left and right.

Hi Joshua - For the release I’ve got underway I’ve taken out the support for landscapeLeft to get around the issue.  Would be nice to find a better work-around in the future hence I’ll try to give you more info below:

* Horizontal scrolling game with gravity, however to get the right dynamics of the player I’m managing his position/velocity myself

* As part of the players speed/direction change it depends on Tilt

* Was hoping to support both landscape directions, to be less annoying to users

* On IOS I have things working for the physics calculations by changing a factor to 1 or -1 depending on orientation (otherwise, in landscapeLeft mode the user would tilt backwards when you tilt the device to the right)

* On Android there seems no way to detect the orientation at all (when I have a landscape locked game) hence this is the issue

I thought it would be good to have both landscapes enabled for IOS, but only one for android, hence my question re build.settings.

Make sense?

I see.  I’m not sure how we can help you solve that problem.  While the accelerometer data may indicate that the device is flipped over, there is no way to know if the app window was actually flipped.  This is especially a problem for devices that do not support flipping to landscapeLeft, making it wrong to assume that the upside down accelerometer values are for a flipped app.

And I’m not jerking you around on this.  The Android OS gives us no clue at the native development side (Java or C/C++) that the app has been flipped.  We’re left in the dark just as much as you are.

I think hard coding your app to be “landscapeRight” is the easiest solution.  That said, the interesting consequence of this is that once your app becomes a “fixed orientation” app, meaning it only supports one orientation, Corona will turn on a sensor and provide an orientation event for all 4 orientations of the device.  At this point, the orientation event does not represent the orientation of your app window, but the orientation of the device.  We’re effectively bypassing Android’s flawed orientation event system and providing our own.  This allows Corona made apps to handle app rotation for themselves.  This also means you can rotate your entire scene and handle this yourself just the way you want.  It’s more work, but it’s the only other solution.

Oh.  I was not aware of the option you mention in your last para - sounds like a work around to still provide the same functionality for IOS without having to have if/then’s in the build.settings then?   

So re “Corona will turn on a sensor and provide an orientation event for all 4 orientations of the device” - then I don’t need to do anything differently in my app here for IOS for this to work?  Still use the orientation change event?