When developing an application for Android in AIR, you may wish to override the default functionality that occurs when a user presses the Back button on their Android device. The default behavior is to exit the application completely, so in many cases it is important to handle this properly. To do so is very easy, and using AIR you can use an event listener like you would for a normal keyboard event.
Here is the code to listen for and override a Back button press on an Android device:
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true)
protected function onKeyDown(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK )
{
event.preventDefault();
event.stopImmediatePropagation();
//handle the button press here.
}
}
Also…..happy 2012 everyone.

Hello,
I was google-ing for “How to prevent auto closing of an app (written in AIR)” and by chance come here. Your idea was the thing which I was using but with no success. Finally I manage to find the problem and the correct solution. Here it is:
systemManager.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onBackButtonPressed);
protected function onBackButtonPressed(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.BACK)
{
event.preventDefault();//prevents auto closing of your application if you are writing Flex based app
}
}
I hope that it will help anyone.
Hi Emil,
Thats unfortunate that it didn’t seem to work for you. I have had success with that method over here though, quite strange!
Good luck with your AIR development.
Craig
Hi,
For me it was working erratically (the handler was called like once every two times).
Could not understand what worked for Emil (removing the listener ?)
Using NativeAppication fixed the problem for me :
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true);
Cheers,
Julien
This is a great discussion. The opening post worked in most cases but there was one part in my app that didn’t work and I have no idea why. However Julien’s line:
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true);
works perfect every time.
Glad that so many people are finding this post useful – I will update the tutorial with your findings . Thanks!
I had a problem with removing views from the stage that currently had focus, so stage.focus became null. As a result, the KeyboardEvents where no longer dispatched by the stage.
To fix this problem I now simply set stage.focus=stage everytime after removing a “major” child.
that seems like a weird problem, what do you class as a major child ? Setting the focus all the time must be quite costly, especially on mobile devices. What phone are you testing on ?
Each event should have a cancelable property. If it’s set to false like in the BACK keyboard event, the preventDefault() command will not work.
yep, same for me, it doesnt work… however from another website I notice the command event.stopImmediatePropagation() which I added then it worked..
// this is to prevent the andreoid button to exit the application
function onKeyDown(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK )
{
event.preventDefault();
event.stopImmediatePropagation();
myText.text = “no BACK no BACK no BACK”;
}
}
Cool thanks ! I have updated the post with the stopImmediatePropagation() method, can see how it would work. I did test this on a couple Android devices, just shows that mobile dev needs a lot of testing!
Note that if you’ve set stage.displayState = FULL_SCREEN, no keyboard events are sent to your app! Use stage.displayState = FULL_SCREEN_INTERACTIVE instead!