I’ve overridden the back button key event plenty of times before, usually to add a “are you sure you want to quit” alert view, and that works fine.
However I’ve just tried using the back button to go back to the main menu (if not already on the main menu), and the app just freezes and then exits.
I have printouts in my createScene, enterScene etc, and they do not print out when I use the back button.
I use storyboard.getCurrentSceneName() in the back button handler and that works fine, and my print(“about to call gotoScene”) call immediately before calling gotoScene works, it’s just the gotoScene function itself that doesn’t work. I have an on screen exit button which works fine (it calls exactly the same function to goto the main menu), so it can’t be that there is an error in either scene.
I’ve noticed in the past that you don’t seem to be able to use gotoScene in an alertview callback function, I wonder if this is the same?
I’ve tried adding a 1 second performWithDelay, and it still doesn’t work.
Here’s my code (the scene names are all prefixed with “scripts.” as they are in a “scripts” folder):
local function onKeyEvent(event) if event.phase == "down" then if event.keyName == "back" then print("pressed back") print("current scene = "..storyboard.getCurrentSceneName()) if storyboard.getCurrentSceneName() == "scripts.createPlayerScene" then print("call gotoScene") timer.performWithDelay(1000, function() storyboard.gotoScene("scripts.mainMenu") end, 1) else local function onComplete(event) if(event.index == 1) then native.requestExit() end end local alert = native.showAlert( "Exit?", "Are you sure you want to exit this app?", { "Yes","No"},onComplete ) end else --no other keys yet end return true else return true end end