Avg. Rating 3.0

Problem

It is necessary to stop the animation and effects in an application at a loss of focus and to resume the animation when you return the focus.

Solution

JSInterface library will be used

Detailed explanation

Nowadays there are many Flash and Flex applications that take a lot of memory and load the processor, even when the user does not work with them. For example, the user can escape to another application or select another tab in browser. A particularly demanding to resources applications that use 3D animation or complex effects and filters, you can unload by stopping the animation, video and music. Thus, to remove the loading from the processor when the user does not work with the application. To obtain the information, when the user does not use application, JavaScript event onblur will be used, that is emitted when the user transfers the focus to another window or tab. The same event - onfocus is used when the user activates the window with the application. JavaScript library is used for interaction with the JSInterface:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

 preinitialize="{preinitializeHandler(event)}">

  <mx:Script>

   <![CDATA[

    import mx.managers.PopUpManager;

    import mx.controls.Alert;

    import aw.external.JSInterface;

    protected function preinitializeHandler(event:Event):void{

     JSInterface.initialize();

     JSInterface.window.onfocus = this.windowOnFocusHandler;

     JSInterface.window.onblur = this.windowOnBlurHandler;

    }

    protected var _focusAlert:Alert;

    protected function windowOnFocusHandler(event:Object=null):void{

     if(!this._focusAlert) return;

     PopUpManager.removePopUp(this._focusAlert);

     this._focusAlert = null;

     // show effects

     // start animation

    }

    protected function windowOnBlurHandler(event:Object=null):void{

     this._focusAlert = Alert.show('Waiting...', '', Alert.OK);

     // stop animation

     // hide effects

    }

   ]]>

  </mx:Script>

</mx:Application>

In this example, an inactive application displays the message "Waiting ...".

Look into attached archive file for example Flex Builder project.

Visit the JSInterface official site.

 

GetWindowFocus.zip
[Sample Flex Builder project]
Report abuse

Related recipes