Navigate back to menu using Back/Return Key on Android

No replies
thinkpozzitive
User offline. Last seen 8 weeks 3 days ago. Offline
Joined: 13 Jan 2012

Hi all,

I'm trying to figure out a way of navigating back to main menu using the android Back/Return key.
Currently I'm using director.lua library to switch from one "scene" (menu) to other scenes "other menus" and I'd like to knwo how I can use
Runtime:addEventListener("key",onKeyEvent) to properly handle navigation.

I guess I'm not fully grasping what's going on, because ussualy the back key will force exit the application.

Typically, you'd do:

1
2
3
4
5
6
7
8
local function onBackButtonPressed(e)
        if (e.phase == "down" and e.keyName == "back") then
            director:changeScene("menu","crossfade")
        end
        return true
    end
 
Runtime:addEventListener( "key", onBackButtonPressed )

However the back button creates 2 events: down and up, and I'm not sure how and when I should remove the event listener, inside which scene, etc.

has anyone got an ideea on how a proper implementation of back button navigation would be done?

Thank you.