Skip to content

Instantly share code, notes, and snippets.

@jgranick
Created February 21, 2012 16:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgranick/1877209 to your computer and use it in GitHub Desktop.
Save jgranick/1877209 to your computer and use it in GitHub Desktop.
How to override the back button on Android (NME recipe)
import nme.display.Sprite;
import nme.Lib;
/**
* @author Joshua Granick
*/
class BackButtonExample extends Sprite {
public function new () {
super ();
Lib.current.stage.addEventListener (KeyboardEvent.KEY_UP, stage_onKeyUp);
}
private function stage_onKeyUp (event:KeyboardEvent):Void {
#if android
if (event.keyCode == 27) {
event.stopImmediatePropagation ();
Lib.exit ();
}
#end
}
}
@jgranick
Copy link
Author

The default behavior for the back button on Android is to minimize your application. Sometimes you may wish to trigger an event before the application goes to the background, or you may wish to close your application entirely.

In NME 3.2, you will need to handle the KEY_UP event for the back button. On older versions you may have to handle both KEY_DOWN and KEY_UP events.

When you call "stopPropagation" or "stopImmediatePropagation" it will cancel the default behavior for the button. Use this to disable minimizing entirely, or in this sample, I am exiting the application instead.

If you wanted to save or perform another behavior before minimizing, you could allow propagation... just handle the event then it will minimize afterwards.

@thomasuster
Copy link

Anyone know if this works in latest NME? Doesn't work for me.

@thomasuster
Copy link

Upgrade to openfl 1.0.4 works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment