I read somewhere that the back button on Android is mapped to the escape key.
So I put in a function to return to the menu screen when pressed.
That seems to work fine but it still closes the app regardless.
I tried putting in event.stopImmediatePropogation but that has no effect.
How do I prevent the default behaviour of the Android back button?
Hereâs my code:
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
private function onKeyUp(e:KeyboardEvent):Void {
if (e.keyCode == 27) {
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation();
audio.stopAll();
for (i in pickups) {
destroy(i);
}
stage.removeEventListener("enterFrame", enterFrame);
parent.removeChild( this );
Main.get.init();
}
}</code>
1 Like
stopImmediatePropagation()
works for me in OpenFL 3.6.1 legacy.
Have you tried adding trace
statements to make sure your code is being run?
Also, have you checked the logs for errors? Maybe youâre blocking the back event, but then your app crashes anyway.
onKeyUp doesnât seem to be firing at all.
I put some traces in there and none of them showed.
Just a bunch of errors saying âcannot run on production devicesâ, âJavabinder failed binder transactionâ, âRemoteException caught trying to send a callback msg for network requestâ.
Donât know what any of these mean.
Those errors sound like something from another app, so Iâd worry about onKeyUp
first.
Try putting a trace
statement right before âLib.current.stage.addEventListener()
,â to make sure thatâs being run.
I added a trace before and after
trace(âaddListenerâ);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
trace("has listener: " + Lib.current.stage.hasEventListener(KeyboardEvent.KEY_UP));
The output said true but none of the traces in onKeyUp are firing.
I even added a textfield and changed the content during onKeyUp, no change visible.
Works fine when compiling to flash
Whatâs Openfl 3.6.1 legacy? Should I be using that?
Joshua described the two modes here. In short, âlegacyâ mode is the old one, and ânextâ mode is the new one, but there are still some things that legacy does better than next.
The reason I specified version 3.6.1 is that legacy mode was removed in OpenFL 4.
1 Like
Wouldnât run at all when i reverted to 3.6.1
Should I add -Dnext to my compiler arguments when running from 4.0.3?
Revert Lime to version 2.9.1 while youâre at it. Also, try a simpler project like DisplayingABitmap (from openfl-samples 3.3.1).
No need. If youâre using version 4.0.3, youâre using next. Thereâs no option to use legacy.
That worked! thank you so much!
There are other differences between OpenFL 3 and 4, and in the long run, it would be better to stay up-to-date.
Since it doesnât look like anyone already reported this bug, Iâd suggest opening an issue. Include the code that causes the error, and maybe they can fix it.
By any chance do you know how I can get my traces to show up on the output panel so I donât have to compile from the command line to get a proper debug mode?
If you are using OpenFL 4, there is a unique key code for âbackâ on Android
import lime.ui.KeyCode;
...
private function stage_onKeyUp (event:KeyboardEvent):Void {
if (event.keyCode == KeyCode.APP_CONTROL_BACK) {
// handle back button
}
}
2 Likes
I do not know if there is a good way to support trace messages on Android with FlashDevelop (that would be up to the FlashDevelop team)
âŚbut we do support a separate trace
command that might be nice. Open a command-prompt, go to your project directory, then run:
openfl trace android
Leave that window open. It will show messages from your application as they are dispatched from your device, and unless I am mistaken, should work at the same time as FlashDevelop
You can also show trace messages in-game.
Neat! Iâll make a note of that for when I upgrade.
This doesnât work when using openfl 4.0.3 and lime 3.0.3
hasEventListener traces as true but the function doesnât fire.
openfl trace android
Works a charm though