Back button listener function exits instead of doing a gotoScene

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 …

I would suspect that you are not setting the value storyboard.returnTo in your scenes.  The code you took makes assumptions that you need to have in place that where you are going to record where you want to go.  You might want to consider a much simpler version of this:

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 == "menu" then         native.requestExit()         return true     else          if storyboard.isOverlay then             storyboard.hideOverlay()             return true         else             storyboard.gotoScene( "menu", g.forward\_trans\_effect, g.trans\_delay )             return true         end     end return false end Runtime:addEventListener( "key", onKeyEvent )

This variant only has two actions (well three, if you count hiding overlays as one).  If you’re not on your menu scene, goto the menu.  If you’re on the menu scene, then request the exit.

Rob

I actually did see on the console that the correct returnTo value was returned, e.g. “previous scene menu” I did try something like what you suggested, last night, but I had the same problem of it exiting.

By the way, I am using a Nexus 7 2013 as the test device, if that helps. So it has the virtual back/home/task buttons on the bottom.

I would put in some print statements and watch your adb logcat and see if your event is getting called twice for some reason…

[quote name=“Rob Miracle” post=“229948” timestamp=“1391891956”]I would put in some print statements and watch your adb logcat and see if your event is getting called twice for some reason…[/quote] Hi Rob, thanks for the suggestion. Last night I was noticing the problem you mentioned, where I would press Back in a second-level scene, then it would go back to the menu and the console indicated another “back” was registered (even though I only pressed once). So it did exit in that case. To get around that, I moved the event function from my current scene to main.lua. When the event code is in main.lua, the console only shows the Back button was pressed once. When I get home I will try your alternate suggestion and put that in one of my scenes and see if it works. I will report back.

I highly recommend that you only set this up in your main.lua.  If you do it in your scenes you risk having multiple handlers for the keyboard.

Rob

It all works now!! I was looking at your code, and realized I was not returning ‘true’ after going to the applicable scene. The ‘return true’ made all the difference.

Thanks for your help Rob.

I would suspect that you are not setting the value storyboard.returnTo in your scenes.  The code you took makes assumptions that you need to have in place that where you are going to record where you want to go.  You might want to consider a much simpler version of this:

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 == "menu" then         native.requestExit()         return true     else          if storyboard.isOverlay then             storyboard.hideOverlay()             return true         else             storyboard.gotoScene( "menu", g.forward\_trans\_effect, g.trans\_delay )             return true         end     end return false end Runtime:addEventListener( "key", onKeyEvent )

This variant only has two actions (well three, if you count hiding overlays as one).  If you’re not on your menu scene, goto the menu.  If you’re on the menu scene, then request the exit.

Rob

I actually did see on the console that the correct returnTo value was returned, e.g. “previous scene menu” I did try something like what you suggested, last night, but I had the same problem of it exiting.

By the way, I am using a Nexus 7 2013 as the test device, if that helps. So it has the virtual back/home/task buttons on the bottom.

I would put in some print statements and watch your adb logcat and see if your event is getting called twice for some reason…

[quote name=“Rob Miracle” post=“229948” timestamp=“1391891956”]I would put in some print statements and watch your adb logcat and see if your event is getting called twice for some reason…[/quote] Hi Rob, thanks for the suggestion. Last night I was noticing the problem you mentioned, where I would press Back in a second-level scene, then it would go back to the menu and the console indicated another “back” was registered (even though I only pressed once). So it did exit in that case. To get around that, I moved the event function from my current scene to main.lua. When the event code is in main.lua, the console only shows the Back button was pressed once. When I get home I will try your alternate suggestion and put that in one of my scenes and see if it works. I will report back.

I highly recommend that you only set this up in your main.lua.  If you do it in your scenes you risk having multiple handlers for the keyboard.

Rob

It all works now!! I was looking at your code, and realized I was not returning ‘true’ after going to the applicable scene. The ‘return true’ made all the difference.

Thanks for your help Rob.