Skip to content

Detecting when a WindowedApplication is activated or deactivated in Adobe AIR

by Peter deHaan on December 20th, 2008

The following example shows how you can detect when an Adobe AIR application gains or loses focus by listening for the windowActivate and windowDeactivate events.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://airexamples.com/2008/12/20/detecting-when-a-windowedapplication-is-activated-or-deactivated-in-adobe-air/ -->
<mx:WindowedApplication name="WindowedApplication_applicationActivate_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        windowActivate="windowedApplication_windowActivate(event);"
        windowDeactivate="windowedApplication_windowDeactivate(event);">
 
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.AIREvent;
            import mx.managers.PopUpManager;
 
            private var alert:Alert
 
            private function windowedApplication_windowActivate(evt:AIREvent):void {
                lbl.text = evt.type;
                if (alert) {
                    PopUpManager.removePopUp(alert);
                }
            }
 
            private function windowedApplication_windowDeactivate(evt:AIREvent):void {
                lbl.text = evt.type;
                alert = Alert.show("WindowedApplication deactivated");
            }
        ]]>
    </mx:Script>
 
    <mx:Label id="lbl" fontSize="32" />
 
</mx:WindowedApplication>
No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS