Hi again everyone.
I’m having problems handling the back button on the device being pressed twice quickly.
If you press the back button again while my code is still handling the first press it crashes the app.
I’ve added a global runtime listener (for obvious reasons) in my main.lua which checks for the back key press which then requires the “QuitAppExit” module which simply puts a message on the screen “Progress has been saved, really quit the game?” with a yes and a no button choice.
Here’s the part of the code in my main.lua that handles the devices back button.
local checkExit, quitApp, checkTimer, preCheck, removeQuitApp, reallyQuitApp, onKeyEvent local firstPress = false function onKeyEvent( event ) if ( "back" == event.keyName and event.phase == "down" ) then if firstPress == false then quitApp = require("QuitAppExit") quitApp.activateButtons() checkTimer = timer.performWithDelay( 500, preCheck, 1 ) firstPress = true else reallyQuitApp() end return true end return true end preCheck = function() timer.cancel( checkTimer ) checkTimer = nil Runtime:addEventListener( "enterFrame", checkExit ) end checkExit = function() if quitApp then if quitApp.noButton.isVisible == false then quitApp.hideExit() Runtime:removeEventListener( "enterFrame", checkExit ) checkTimer = timer.performWithDelay( 1000, removeQuitApp, 1 ) end if quitApp.yesButton.isVisible == false then quitApp.hideExit() Runtime:removeEventListener( "enterFrame", checkExit ) checkTimer = timer.performWithDelay( 1000, reallyQuitApp, 1 ) end end end removeQuitApp = function() Runtime:removeEventListener( "key", onKeyEvent ) timer.cancel( checkTimer ) checkTimer = nil quitApp.remVar() package.loaded["QuitAppExit"] = nil quitApp = nil end reallyQuitApp = function() Runtime:removeEventListener( "key", onKeyEvent ) timer.cancel( checkTimer ) checkTimer = nil quitApp.remVar() package.loaded["QuitAppExit"] = nil quitApp = nil system.setIdleTimer( true ) package.loaded["physics"] = nil physics = nil package.loaded["socket"] = nil socket = nil package.loaded["loadsave"] = nil loadsave = nil display.setStatusBar(display.DefaultStatusBar) storyboard.removeAll() native.requestExit() end Runtime:addEventListener( "key", onKeyEvent )
I’ve experimented with different ways to try and stop the double tap on the back button crashing the app but I’m all out of ideas, has anybody here had this issue in the past and can give me some good advice?
Thanks!