It's a little trick, but I've solved it.
First, if the orientation has to change, I save the old orientation. When I click the button that allow orientation change again, first I put the old orientation and then, I allow auto-orientation.
This is part of the new code:
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
}
The button to allow autoorientation:
if(oldOrientation != null){
stage.setOrientation(oldOrientation);
oldOrientation = null;
}