Android device back button utilizing Director class

Hi, I’m using the Key Event to detect when a user presses the Android device’s back button:
http://developer.anscamobile.com/reference/index/events/key

Basically, I want the Back button event to take the user back to screen 1 (screen1.lua). Does anybody know what the code is to get this to work? [import]uid: 82194 topic_id: 13985 reply_id: 313985[/import]

if should be something like this

[lua]-- Key listener
local function onKeyEvent( event )
local phase = event.phase
local keyName = event.keyName
eventTxt.text = “(”…phase…" , " … keyName …")"
if keyName == “back” then – am not too sure about the name of the key
director:changeScene(“mainMenu”)
end
return true
end

– Add the key callback
Runtime:addEventListener( “key”, onKeyEvent )[/lua] [import]uid: 71210 topic_id: 13985 reply_id: 51520[/import]

Thanks for the sample code! I tried it out, but unfortunately could not get it to work. However, after doing more research, I discovered this thread here:
http://developer.anscamobile.com/forum/2011/07/13/build-569

Supposedly, you cannot call a scene within the back key listener. Here is the final code that I used to get it to work on my HTC Desire S. It will send a user back to Screen 1 (screen1.lua) after the user presses the Back button on their Android device. I’m posting the final working code below to help others if they have the same problem as I did.

[code]
module(…, package.seeall)


– GROUPS

local localGroup = display.newGroup()


– BACK BUTTON FLAG

local backButtonPushed = false


– LISTENERS

local function animate(event)
if backButtonPushed == true then
backButtonPushed = false
director:changeScene(“screen1”)
– or
– os.exit()
end
end

local function onKeyEvent( event )
local phase = event.phase
local keyName = event.keyName

if phase == “up” and (keyName == “back”) then
backButtonPushed = true
end

return true
end


– INIT VARS

local function initVars ()


– Listeners

Runtime:addEventListener( “key”, onKeyEvent )
Runtime:addEventListener( “enterFrame”, animate )

end


– CLEAN

function clean ( event )
Runtime:removeEventListener( “key”, onKeyEvent )
Runtime:removeEventListener( “enterFrame”, animate )
backButtonPushed = nil
end


– NEW

function new()


– Initiate variables

initVars()


– MUST return a display.newGroup()

return localGroup

end
[/code] [import]uid: 82194 topic_id: 13985 reply_id: 51658[/import]

oh…is it some kind of global event which is within the scope of the whole app ?
hmmm need to do some research on this…

any way thanks for the code and the link…
:slight_smile:
[import]uid: 71210 topic_id: 13985 reply_id: 51659[/import]

Hmm, that’s a good point. This device back button event only works on the single page that is coded with it. Does anybody know the coding to make this event automatically work on ALL pages within the app? [import]uid: 82194 topic_id: 13985 reply_id: 51683[/import]

@djtan42 is it only for single scene ? I thought I read its kind of global. Frankly speaking I haven’t tried it. did you try doing that ? [import]uid: 71210 topic_id: 13985 reply_id: 51686[/import]

Rather than adding an enter frame listener you can also use the timer.performWithDelay
Here is a snippit that seems to be working:

[code]
local function onKeyEvent( event )
local phase = event.phase
local keyName = event.keyName

logit("onKeyEvent: " … tostring(phase) … " " … tostring(keyName))

if (keyName == “back”) then
if (phase == “up”) then
timer.performWithDelay(10, handleBackKeyReleased)
end

return true
end
end

[/code] [import]uid: 118012 topic_id: 13985 reply_id: 135887[/import]

Rather than adding an enter frame listener you can also use the timer.performWithDelay
Here is a snippit that seems to be working:

[code]
local function onKeyEvent( event )
local phase = event.phase
local keyName = event.keyName

logit("onKeyEvent: " … tostring(phase) … " " … tostring(keyName))

if (keyName == “back”) then
if (phase == “up”) then
timer.performWithDelay(10, handleBackKeyReleased)
end

return true
end
end

[/code] [import]uid: 118012 topic_id: 13985 reply_id: 135887[/import]