Correct orientation avoidance in Flex Mobile app

Can anyone make it work correctly in Flex SDK 4.6?

Here is a short snippet:

<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" addedToStage="onAddedToStage(event)" title="Title"> <fx:Script> <![CDATA[ private function onAddedToStage(event:Event):void { if (stage.autoOrients) { stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging, false, 0, true); } } private function orientationChanging(event:StageOrientationEvent):void { if (event.afterOrientation == StageOrientation.DEFAULT || event.afterOrientation == StageOrientation.UPSIDE_DOWN) { event.preventDefault(); } } ]]> </fx:Script> </s:View> 

What I'm trying to achieve is to maintain landscape mode in both orientations, so if the user rotates the device 180 degrees, the screen should also rotate. But in general, there should be no action when the user rotates the device to one of the portrait orientations. Instead, I see width changes in the navigator action bar, and sometimes content in portrait orientations, so apparently preventing the event is not enough. I use the "official" method that Adobe offers, but the problem is that it does not work very well. Of course, the stage does not change, but something seems to work in the navigator, since you can see the change in the width of the action bar.

I had some success in explicitly setting layoutbounds to a fixed width in the handler method - this prevents the width of the action bar from changing, but this is only a temporary solution - if the view is an object of a transition or other redrawing - it will again render with poor sizes. As if there was something lower, it said that it was in portrait mode, although I am trying to prevent it.

Before blowing yourself up with stupid ideas like "autoOrient = false", don't. This is clearly not a solution to this problem. Obviously, this is a bug with the Flex SDK - did anyone find a way to fix this or a stable workaround?

EDIT: others seem to have faced a similar problem:
- http://forums.adobe.com/message/3969531 (the main topic is about something else, but read the comment on magic robots)
- http://forums.adobe.com/message/4130972

+4
source share
2 answers

I'm not sure if this is right, I did something wrong at the end, but after a big fight, I found this to be a stable solution:

 private function onAddedToStage(event:Event):void { if (stage.autoOrients) { stage.removeEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging); stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging, false, 100, true); } } private function orientationChanging(event:StageOrientationEvent):void { event.stopImmediatePropagation(); if (event.afterOrientation == StageOrientation.DEFAULT || event.afterOrientation == StageOrientation.UPSIDE_DOWN) { event.preventDefault(); } } 

First of all, it should be noted that addToStage works several times (2-3) in the mobile application. I do not know why, in the code there is no addChild. Perhaps the AIR runtime is doing something else. Thus, in order to avoid adding an unnecessary number of handlers, the general technique is to remove the handler first - it will not do anything if such a handler is not registered yet, but if it is, it will delete it, which will keep the handler counter at 1.

Secondly, the priority of the event is that it will not work at 0, it must be set to something big in order to be launched in front of the file during AIR execution.

The last thing is event.stopImmediatePropagation () - now that we are the first to process the event, we cannot prevent this event, which will be sent further in this particular scenario.

This together makes an orientation that prevents impeccable work - for me, the landscape and the reverse landscape (rotated_left, rotated_right) worked and switched, and portrait modes did not affect the presentation at all.

Now there is a danger: you can remove the listener after exiting the view (at the end of the transition animation, deactivate the view or something else), because stopImmediatePropagation will prevent the event from being processed in other parts of your application.

I hope that Adobe (or Apache now, actually) will look more closely at this problem and track my solution.

EDIT

Here's the last problem with this solution, if the application starts when the device is in the DEFAULT or UPSIDE_DOWN orientation, in this case the application will be in portrait mode.

To fix this, you need to change the aspect ratio in the addToStage handler:

  if(Stage.supportsOrientationChange) { stage.setAspectRatio(StageAspectRatio.LANDSCAPE); } 
+6
source

So, I had the same problem as yours. I seem to finally understand the solution. Here is what I did:

 <s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" ...blahblahblah... width="1024"/> protected function tabbedviewnavigatorapplication2_applicationCompleteHandler(event:FlexEvent):void { stage.autoOrients=true; preventOrient(); } private function preventOrient():void { if (stage.autoOrients) { stage.removeEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging); stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging, false, 100, true); } } private function orientationChanging(event:StageOrientationEvent):void { if(event.afterOrientation == StageOrientation.DEFAULT || event.afterOrientation == StageOrientation.UPSIDE_DOWN || event.afterOrientation == StageOrientation.UNKNOWN) { event.preventDefault(); } } 

It is worth noting that in the full application handler, I set stage.autoOrients to true, because in the app.xml file I have it set to false, due to the presence of a splash screen and does not want users to reorient the screen during that time. Actually, the only thing I did differently was the StageOrientation.UNKNOWN account and preventing it from doing this, set the width to 1024 (for the iPad screen, it may be different for other tablet devices) in the main mxml file and delete the stop-intermediate Hope this helps.

+1
source

Source: https://habr.com/ru/post/1391526/


All Articles