Back button for android is not working for apk file.
Copy link to clipboard
Copied
I am using Flash CS6 to create an android app. I am trying to code for my back button. It works well when i run the file in flash but after I published it out into my handphone, the back button brings me from page 3 to page 1 instead of from page 3 to page 2. I am doing my coding in the frame itself and not by class.
The code i have used is below:
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.desktop.NativeApplication;
stop();
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler159)
function onKeyDownHandler159(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK)
{
event.preventDefault();
event.stopImmediatePropagation();
NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler159);
gotoAndStop (14);
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler159)
}
}

Copy link to clipboard
Copied
In an AIR Android app I was working on a year ago, I used:
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyboardPress);
this.stage.addEventListener(KeyboardEvent.KEY_UP, _onKeyboardPress);
final private function _onKeyboardPress(e:KeyboardEvent):void {
if (e.type == KeyboardEvent.KEY_DOWN && (e.keyCode == Keyboard.BACK || e.keyCode == Keyboard.ESCAPE)) {
e.preventDefault();
}
else if (e.type == KeyboardEvent.KEY_UP && (e.keyCode == Keyboard.BACK || e.keyCode == Keyboard.ESCAPE)) {
e.preventDefault();
// execute other code
}
}
And that worked fine for me. I used both keyDown and keyUp. KeyDown was for stopping the default action in case it tried to close the app. KeyUp did the same thing but executed my AS code at that point because I felt it was a better experience for the release of the "Back" button to execute the code and not when you press down on it. This code was in my document class file, but I don't see why it would make too much of a difference since Im referencing the Stage and, to my knowledge, there can be only one Stage per app window. I also have the "Escape" key check in there because I used the desktop AIR emulator for my app testing since it was faster to load and I didnt need all the features of the Android emulator such as orientation change.

