Allowing users to correctly exit your application is a must for all apps, otherwise they will stay open behind the scenes chewing up resources (and perhaps chewing through a users data $$$).
Fortunately we can access this functionality though AIR’s NaviveApplication object.

Continue reading to learn how to exit an app and use the soft keys correctly.

Make sure your editor, whether FlashDevelop, FDT or Flash Builder, has correctly linked to the latest stable version of the AIR SDK – and is outputting to AIR. Outputing to FlashPlayer will cause errors during compile.

In it’s simplest form usage would be as follows.

import flash.desktop.NativeApplication

NativeApplication.nativeApplication.exit();

Another scenario would be to use the devices native buttons – BACK, HOME and MENU, to determine our next action.

Device button events can be listened to by accessing Keyboard even key codes (just like in the desktop flash player).


import flash.desktop.NativeApplication
import flash.events.KeyboardEvent;

NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
				
protected function onKeyDown(e:KeyboardEvent):void
{
	if(e.keyCode == Keyboard.BACK)
	{
            //handle the button press here.
        }
	else if(e.keyCode == Keyboard.HOME)
	{
            //handle the button press here. 
        }
	else if(e.keyCode == Keyboard.MENU)
	{
            //handle the button press here.
        }
}

These buttons already have a pre-defined behavior when they are pressed which is determined by the device.
For example, the BACK button will minimize an app but not close it.

To override these default behaviors and create our own, we use the preventDefault() method.

See usage example below


protected function onKeyDown(e:KeyboardEvent):void
{
	if(e.keyCode == Keyboard.BACK)
	{
             //stop default behaviour
             e.preventDefault();

             // if app is currently in play state, change to menu state
             if(GameState == PlAY)
             setGameState(MENU);
             
             //if app is currently in the Menu state, exit the app.
             else if(GameState == MENU)
             NativeApplication.nativeApplication.exit();

        }
}

The same process can be used to map the MENU and HOME buttons, however based on the HOME button is protected by the system and tis behavior cannot be altered.

The HOME button however dispatches the Deactivate event. The same event is fired when the user presses the screen lock button, or if the device receives a phone call.
on the iphone the home button specifically dispatches an EXIT event, so for good measure we will listen to both.

When these events are fired, the ideal scenario is to pause the game and kill any sounds, gps, accleromiter, heavy resouces.

When the app is returned to after these events, an Activate event is dispatched which we can listen to and handle with a method for a resuming user.

NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onDeactivate);
NativeApplication.nativeApplication.addEventListener(Event.EXITING, onDeactivate); 
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, onActivate);

function onDeactivate(e:Event):void 
{
//cleanup();
//changeState(PAUSE_GAME);			
}

function onActivate(e:Event):void {
//Code to be executed to resume the game.
}