Trouble with Android BACK key

I’m trying to capture the BACK key on the Android and have it control navigation, but I’m running into some issues.  It SEEMS to work… but it takes me back to the main menu and then exits the app.  I just want it to take me back to the previous screen - in this case the main menu - which I am controlling manually.

It’s almost as if it handles the key press… gets back to the main menu… then handles it again from the main menu and closes the app.

I have no handler at the moment in the main menu, but here is the code from the previous screen.

onHardwareKeyEvent = function( event ) local returnValue = false -- use default key operation local phase = event.phase local keyName = event.keyName if ( phase == "up" and keyName=="back" ) then if (ShowingDoc == false) then -- not showing a document so go to menu Runetime:removeEventListener( "key", onHardwareKeyEvent ) storyboard.gotoScene( "scene\_menu" ) else -- showing a document so kill it webView:removeSelf() webView = nil topText.text = "Protocols" topText.size = 18 ShowingDoc = false end end return returnValue end Runtime:addEventListener( "key", onHardwareKeyEvent )

Have you tried changing the returnValue from false to true?

Hey thanks!  That worked!  What’s the difference?

return true

basically tells lua “This event has been handled, no need to do anything else”.

Returning false (or not returning anything at all), means that an event may propagate through other functions until it has been handled. 

A more common time you may come across this is in “touch” listeners. If you have 2 objects where one is drawn directly in front of the other, and they both have touch listeners, pressing on the front object will not necessarily mean that the touch will not also occur on the back one. As a general rule, I put “return true” at the bottom of all of my touch/tap/key listeners.

From the menu, I go to secondary screens.  From the secondary screens, the back button works and takes me back to the main menu.  BUT… if I go to from a secondary screen to a subscreen, it jumps me back to the the main menu even though I specify go to a specific (the secondary) scene.

Because I have to turn things on and off (with native components), I have to have a back button handler for every scene.  So each scene has one.  Before switching to another scene, I am turning removing the handler.  I’m using local functions and remove the handler but I don’t know why it isn’t working in the others scenes even tho I’m using nearly the same code.

Is there a better way?  Well… better or not… a way that will actually works?

Is there ANY WAY to test this from the simulator?  It takes me 12 minutes to compile, build and load onto my app.

local function onBackButtonPressed(e) if (e.phase == "down" and e.keyName == "back") then --Here the key was pressed downPress = true return true else if (e.phase == "up" and e.keyName == "back" and downPress) then downPress = false if (ShowingDoc == false) then -- not showing a document so go to menu Runtime:removeEventListener( "key", onBackButtonPressed ); storyboard.gotoScene( "scene\_references" ) else -- showing a document so kill it if webView then webView:removeSelf() webView = nil end topText.text = "Trauma Triage Criteria" topText.size = 16 ShowingDoc = false end return true end end return false; end Runtime:addEventListener( "key", onBackButtonPressed )

Have you tried changing the returnValue from false to true?

Hey thanks!  That worked!  What’s the difference?

return true

basically tells lua “This event has been handled, no need to do anything else”.

Returning false (or not returning anything at all), means that an event may propagate through other functions until it has been handled. 

A more common time you may come across this is in “touch” listeners. If you have 2 objects where one is drawn directly in front of the other, and they both have touch listeners, pressing on the front object will not necessarily mean that the touch will not also occur on the back one. As a general rule, I put “return true” at the bottom of all of my touch/tap/key listeners.

From the menu, I go to secondary screens.  From the secondary screens, the back button works and takes me back to the main menu.  BUT… if I go to from a secondary screen to a subscreen, it jumps me back to the the main menu even though I specify go to a specific (the secondary) scene.

Because I have to turn things on and off (with native components), I have to have a back button handler for every scene.  So each scene has one.  Before switching to another scene, I am turning removing the handler.  I’m using local functions and remove the handler but I don’t know why it isn’t working in the others scenes even tho I’m using nearly the same code.

Is there a better way?  Well… better or not… a way that will actually works?

Is there ANY WAY to test this from the simulator?  It takes me 12 minutes to compile, build and load onto my app.

local function onBackButtonPressed(e) if (e.phase == "down" and e.keyName == "back") then --Here the key was pressed downPress = true return true else if (e.phase == "up" and e.keyName == "back" and downPress) then downPress = false if (ShowingDoc == false) then -- not showing a document so go to menu Runtime:removeEventListener( "key", onBackButtonPressed ); storyboard.gotoScene( "scene\_references" ) else -- showing a document so kill it if webView then webView:removeSelf() webView = nil end topText.text = "Trauma Triage Criteria" topText.size = 16 ShowingDoc = false end return true end end return false; end Runtime:addEventListener( "key", onBackButtonPressed )