Hi, I’ve just started testing the Back button functionality with my app on Android, but every time I press Back it just exits the app. I’m using similar code to what was in the helpful “Android-izing” tutorial for the event listener.
In my main.lua:
local function onKeyEvent(event) local phase = event.phase local keyName = event.keyName print( event.phase, event.keyName ) if ( "back" == keyName and phase == "up" ) then if storyboard.currentScene == "splash" then native.requestExit() else if storyboard.isOverlay then storyboard.hideOverlay() else local lastScene = storyboard.returnTo print( "previous scene", lastScene ) if lastScene then print("going to scene") storyboard.gotoScene( lastScene, g.forward\_trans\_effect, g.trans\_delay ) else native.requestExit() end end end end return false end Runtime:addEventListener( "key", onKeyEvent )
I have checked the Logcat output and it does show that it went to the correct code path in my listener function (it spits out my print statement: ‘going to scene’). It seems that when it calls the ‘gotoScene’, it exits instead of going to the scene.
In the createScene of my menu.lua I had set the ‘returnTo’ variable as such:
storyboard.returnTo = nil (here it will exit, of course)
And in my later scenes:
storyboard.returnTo = “menu” (or whatever I had for the previous scene name)
Any ideas what I’ve done wrong? I’ve search around but haven’t found an answer. My workaround for the moment is to pop up an “Exit yes/no” dialog …