how to stop a game from closing when back button is pressed?

Okay so i am having some problems with the closing of a game on android. On iphone there is only one button so it doesnt matter because the app can be closed from that button anyway, but on android when back button is pressed instead of going to the previous scene the game window just closes. Any help on this issue will be much appreciated.

You need to catch the back button pressed and then manage it as appropriate for your app. See below code snippet. This goes in your main.lua and it has storyboard calls. If you are using the newer composer you can adjust the calls appropriately. Hope this helps.

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.isOverlay ) then print("scene is overlay") storyboard.hideOverlay() else local lastScene -- code here to determine where you want back button to take you if lastScene then -- if there is a prior scene in your app that you want to go back to ... local options = { effect = "crossFade", time = 200 } storyboard.gotoScene( lastScene, options ) end else -- no prior scene to go back to. We were already at the top so lets exit. print("attempting to exit to OS") native.requestExit() end end return true end return false end Runtime:addEventListener( "key", onKeyEvent )

You need to catch the back button pressed and then manage it as appropriate for your app. See below code snippet. This goes in your main.lua and it has storyboard calls. If you are using the newer composer you can adjust the calls appropriately. Hope this helps.

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.isOverlay ) then print("scene is overlay") storyboard.hideOverlay() else local lastScene -- code here to determine where you want back button to take you if lastScene then -- if there is a prior scene in your app that you want to go back to ... local options = { effect = "crossFade", time = 200 } storyboard.gotoScene( lastScene, options ) end else -- no prior scene to go back to. We were already at the top so lets exit. print("attempting to exit to OS") native.requestExit() end end return true end return false end Runtime:addEventListener( "key", onKeyEvent )