I am trying to implement Android back button functionality in my app, but having trouble due to the order the event listeners fire on Runtime.
If I add this to a scene:
local function onKeyEvent( event ) -- If the "back" key was pressed on Android, then prevent it from backing out of your app. if (event.keyName == "back") then -- go back or do something here return true end -- Return false to indicate that this app is \*not\* overriding the received key. -- This lets the operating system execute its default handling of this key. return false end Runtime:addEventListener( "key", onKeyEvent );
Then add an overlay (the menu for example), I would like to add similar code to the menu, so that the back button only hides the menu. However, when I do that, the *first* onKeyEvent function that was defined is hit first, which is in the scene, not in my overlay.
Is there a way to set the precedence of the event listeners? Ultimately, something like this:
Runtime:insertEventListener(“key”, onKeyEvent, 1) – add this event as the first one, shifting all others
or just an overload to add:
Runtime:addEventListener("key, onKeyEvent, 1)
That way, it would always hit the “latest” function that was defined for handling the back button, and I can stop it from going any further.
I could do what I want by inserting into the _functionListeners directly I guess, but I’m always hesitant to mess with undocumented stuff in case Corona changes a variable name down the road.
Thanks,
Dave