Tom Krcha's FlashRealtime

Hey amigo!
I am Tom Krcha, Gaming Evangelist at Adobe. These are my notes

Tip: Close Your Android AIR App on Back Button

October 13th, 2010

While developing AIR for Android apps, you should consider whether you need your apps to run in the background, especially if they are highly intensive graphic games.

To make your users even happier, it’s a good idea to close your app when the Back or Home button is pressed to free CPU and memory. Multitasking is a great thing, but if your app doesn’t need to be open, it’s better to close it and maybe save the state of your application. You can read this article on saving state in AIR applications for iOS devices (same should work for Android).

My fellow evangelist Mark Doherty sent me this code, which does the job. It can be helpful to you, so sharing:

Register handlers:

	NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate, false, 0, true);
	NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true);
	NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);

Define handlers:

private function handleActivate(event:Event):void
	NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
private function handleDeactivate(event:Event):void
private function handleKeys(event:KeyboardEvent):void
	if(event.keyCode == Keyboard.BACK)

Good luck!

Facebook comments:


  1. I just posted a follow up to your tip to show some gotcha’s when using the CameraUI or CameraRoll and nativeApplication.exit(). See the details at

    Comment by Rich Tretola — October 13, 2010 @ 3:48 pm

  2. This is a good technique to be clean-but is there not a way to capture the back button and have your app respond to it?
    I have a keyboard listener that successfully captures the menu… and even he back but that’s only immediately prior to the device navigating to the previous app. In addition, I’ve tried the uses-permission FORCE_BACK but it still doesn’t work. Please advise.

    Comment by Phillip Kerman — December 21, 2010 @ 8:47 pm

  3. This works but there is no way to stop the default back button

    Comment by Almog — August 12, 2011 @ 7:17 pm

  4. way to handle back button
    protected function handleKeys(event:KeyboardEvent):void

    Comment by Georgi Gevorgyan — March 18, 2012 @ 5:49 pm

  5. I might add Internet play next and I’m checking out the options for a server.

    Comment by Louis Vuitton Handbags Outlet — August 14, 2012 @ 11:33 am

  6. tom, i got error when use this code
    for static NativeApplication.nativeApplication
    it says:
    1119: Access of possibly undefined property ACTIVATE through a reference with static type Class.
    any clue?

    Comment by Ruly — August 17, 2012 @ 3:32 pm

RSS feed for comments on this post. / TrackBack URL

Leave a comment

Comment moderation is enabled. Your comment may take some time to appear.