Android back button

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?

When using FlashDevelop

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