Skip to main content
March 11, 2013
Question

Flex mobile orientation: force portrait mode and then allow auto orientation

  • March 11, 2013
  • 3 replies
  • 3682 views

I'm creating a flex mobile project and I want to force the app to portrait orientation when I click a button, and when I click other button allow again to change the orientation.

This is my code when I click the first button, where I want to force portrait mode:

protected function click(event:MouseEvent😞void{

     if(stage.orientation != StageOrientation.DEFAULT){

          stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged);

          stage.setOrientation(StageOrientation.DEFAULT);

     }else{

          doSomething();

     } 

}  


private function orientationChanged(event:StageOrientationEvent😞void{

     doSomething();

     stage.removeEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged);

} 


private function doSomething():void{

     stage.autoOrients = false;

}

It works ok and it changes the orientation if it's needed.

Now, when I want to allow orientation change again, I've only putted:

stage.autoOrients = true;

It works ok if when I click the first button the app is in portrait and it doesn't have to change anything. But if it's on landscape and it have to change to portrait, when I allow orientation change again, it doesn't work ok.

Do you know if I have to allow or change something? Or, is there any better way to do this?

Thanks in advance

This topic has been closed for replies.

3 replies

Adobe Employee
March 12, 2013

Instead of setting the orientation, try setting the aspect ratio to portrait. When autoOrients is true and aspect ratio is PORTRAIT, both PORTRAIT and PORTRAIT_UPSIDE_DOWN orientations are supported. Setting the aspect ratio to PORTRAIT would change the stage orientation to PORTRAIT if the application is currently in landscape mode.

More details available at http://blogs.adobe.com/airodynamics/2012/05/22/stage-aspectratio-enhancements/

March 12, 2013

Thanks for your answer. It works, but I have the same problem that I have setting the orientation. If I'm on landscape, it changes to Portrait ok, but when I want to allow auto orientation again, it doesn't work well.

A little tricky I've found is:

When I want to force to portrait:

private var oldOrientation:String = null;

protected function click(event:MouseEvent):void{
  
if(stage.orientation != StageOrientation.DEFAULT){
            oldOrientation
= stage.orientation;
            stage
.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged);
            stage
.setOrientation(StageOrientation.DEFAULT);
  
}else{
            doSomething
();
  
}

}


private function orientationChanged(event:StageOrientationEvent):void{
           
// do something if you are changing to portrait mode (the first change) and other thing if you are changing to old orientation
}

And when I want to allow auto orientation again:


if(oldOrientation != null){

        stage
.setOrientation(oldOrientation);
        oldOrientation
= null;             
}

Adobe Employee
March 12, 2013

When you want to allow auto orientation, just setting autoOrients to true would allow the app to rotate automatically only in portrait orientations. In case you want to the stage to auto rotate to all orientations, set aspect ratio to ANY and auto orients to true. Now stage would be able to rotate to all orientations automatically.