I have an AIR application that I want to lock in landscape orientation. I never want the app to rotate to portrait mode. Adobe's solution for this problem would seemingly be to configure my * -app.xml file as follows:
<aspectRatio>landscape</aspectRatio> <autoOrients>false</autoOrients>
This works most of the time, but not always. For example, in some versions of Android (example: 2.x, running on the Nook plank), if the screen of my device turns off when Flash Builder packs and deploys my APK, the application starts in a dilapidated state, where its aspect ratio is landscape, but everything is measured with portrait sizes. On other devices, such as Google Nexus (Android 4.1), the application starts correctly (in landscape mode), but if I turn off the screen, rotate the device in portrait mode and turn on the screen again - my AIR application was invisibly turned back to portrait mode. Even worse, StageOrientationEvent.ORIENTATION_CHANGE events are not even dispatched to indicate that this has happened.
I tried a variant of the above approach, which is to programmatically determine the orientation of the device:
private function onAddedToStage( event:Event ):void { stage.autoOrients = false; stage.setOrientation( StageOrientation.ROTATED_RIGHT ); }
This approach has the same problems discussed above.
I also tried the approach mentioned in this other thread . Instead of disabling auto orientation, I tried to listen to StageOrientationEvent.ORIENTATION_CHANGE and prevent the device from switching to portrait mode:
private function onAddedToStage( event:Event ):void { stage.addEventListener( StageOrientationEvent.ORIENTATION_CHANGE, onStageOrientationChange, true, int.MAX_VALUE ); } private function onStageOrientationChange( event:StageOrientationEvent ):void { switch( event.afterOrientation ) { case StageOrientation.DEFAULT: case StageOrientation.UPSIDE_DOWN: case StageOrientation.UNKNOWN: event.preventDefault(); break; case StageOrientation.ROTATED_RIGHT: case StageOrientation.ROTATED_LEFT: break; } }
This, too, seems to work only for a while. In many cases, event.preventDefault () has no effect, and the application rotates anyway.
So ... my question is to other AIR developers: does anyone know how to securely lock the device in one orientation?