Android "back" key.

Hi everyone!
I need your help with this:

When “back” key is pressed (in Android), I want to back to menu if I’m in a level scene, and I want to close the app if I’m in the menu.

This is my code:

function OnKeyEvent (event) local KeyName = event.keyName if (KeyName == "back") then GoToMenu() -- or os.exit() return true else return false end end Runtime:addEventListener("key", OnKeyEvent)
It doesn’t work.

Also, I tried this:

[code]local BackButtonPushed = false

function Animate (event)
if BackButtonPushed == true then
BackButtonPushed = false
GoToMenu()
– 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

Runtime:addEventListener(“key”, OnKeyEvent)
Runtime:addEventListener(“enterFrame”, Animate)[/code]
It doesn’t work either.

I think the problem is that OnKeyEvent function doesn’t stop the back key action.

I’ve made the following test:

function OnKeyEvent (event) local KeyName = event.keyName if (KeyName == "back") then local txtTest = display.newText("Back button is pushed", 100, 100, native.systemFont, 50) return true else return false end end Runtime:addEventListener("key", OnKeyEvent)
When I hit back key I can see txtTest, but immediately after the app is closed (or minimized).

My device is HTC Sensation with Android 4.0.
I work with build 813 for Windows.

Thanks in advance.
Rafael. [import]uid: 104648 topic_id: 26656 reply_id: 326656[/import]

I too have an issue with this, I have a single user interface design which employs an on-screen back button to navigate backwards through a stack, however on android devices, hitting back will simply take the user back to their app list or home screen (wherever they launched from) rather than providing useful in-game navigation opportunities. The Home button on android devices has this effect so it would be awesome if we could intervene with the back button messages and as suggested above, step-back (n) times before finally exiting?

I have searched this topic and as demonstrated by Rafael above, the device simply exits regardless of being told that the message is handled.

Thanks [import]uid: 51222 topic_id: 26656 reply_id: 108114[/import]

I have implemented the ‘back’ (and ‘menu’ and ‘search’) button in one of my apps and it works fine. I glanced through your code here and the only thing I’d change is collapse the space between the function name and the parentheses.

[lua]Function(param)[/lua]

not

[lua]Function (param)[/lua]

Here’s my code.

[lua]function androidKeyHandler(event)
if director:isPopUpActive() then return true end
if event.phase == “down” then
if event.keyName == “back” then
event.phase = “release”
endGame(event) --dispatches event to another listener
elseif event.keyName == “search” then
–do a search thing
elseif event.keyName == “menu” then
if ( not popUpActive ) then
–popup a popup thing
end
else
return false
end
end
return true
end[/lua] [import]uid: 44647 topic_id: 26656 reply_id: 108134[/import]

Hi all!
First let me thank you for your answers.

I modified my code based on yours:

function androidKeyHandler(event) if director:isPopUpActive() then return true end if event.phase == "down" then if event.keyName == "back" then event.phase = "release" Menu() else return false end end return true end Runtime:addEventListener("key", androidKeyHandler)
But unfortunately, the result is the same. The device exits.
What’s wrong? [import]uid: 104648 topic_id: 26656 reply_id: 108216[/import]

Take a look at this thread. I’ve given an example here on how to properly handle the back button to not exit the application: http://developer.anscamobile.com/forum/2012/02/17/disable-android-back-key [import]uid: 117906 topic_id: 26656 reply_id: 113587[/import]

@toby2 thanks for the idea to write event.phase = “release”, it’s great [import]uid: 172733 topic_id: 26656 reply_id: 124347[/import]

@toby2 thanks for the idea to write event.phase = “release”, it’s great [import]uid: 172733 topic_id: 26656 reply_id: 124347[/import]