Handling the Back button in AIR for Android

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. ;-)

FacebookTwitterGoogle+LinkedInRedditStumbleUpontumblrEmail

15 thoughts on “Handling the Back button in AIR for Android

  1. 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.

    1. 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

  2. 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

    1. 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.

  3. 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.

  4. 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 ?

  5. 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.

  6. 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”;

    }
    }

  7. 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!

  8. Doesn’t seem to work. Did they change something? Is there a workable solution? I have been searching the web for hours now.

    1. I was having some trouble with this too. I’m using flash develop, and the starter template set up the deactivate function which calls.

      NativeApplication.nativeApplication.exit();

      Commenting that line out, or checking for conditions if I want the app to exit, seems to solve the problem for me.

  9. hi,

    I want Handel back button like:

    when user click back button on android device the previous scene should open but when it reach to home screen application should close.

    plz help to achive this I m little new to as3….

    thanks

  10. HI….how to use mobile “back button” and appear a box asking “yes or no” …”yes” for exit completely and “no”for back to system… hope u can understand my question and can help me…thank a lot :D

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>