Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Going Back to a scene with Android 'Back' Button
Started by karthik.cvr70 Sep 02 2016 09:41 AM

1 reply to this topic
scene android back button
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 2 posts
  • Corona SDK

Hi. I wrote a code where if a user clicks on help button in menu, it goes to a scene where he can read the help info. When he clicks the back button, its supposed to go back to menu.

This is how I wrote the code in menu.lua for going to help screen. It worked fine.

local function gotoHelp()
    composer.removeScene( "help" )
    composer.gotoScene( "help" )
helpButton:addEventListener( "tap", gotoHelp )

Now I wrote the code in help.lua in such a way that the user goes back to menu once he presses the back button.

-- Called when a key event has been received
local function onKeyEvent( event )
    -- If the "back" key was pressed on Android or Windows Phone, make sure it gets back to menu
    if ( event.keyName == "back" ) then
        local platformName = system.getInfo( "platformName" )
        if ( platformName == "Android" ) or ( platformName == "WinPhone" ) then
            composer.removeScene( "menu" )
            composer.gotoScene( "menu" )
    return false
Runtime:addEventListener( "key", onKeyEvent )

But this one didn't work. The game is just getting stopped from being exited but nothing else is happening.

Can anyone please help me?

[TOPIC: post.html]

Ajay McCaleb

[GLOBAL: userInfoPane.html]
Ajay McCaleb
  • Contributor

  • 209 posts
  • Corona Staff

Hi karthik.cvr70,


In your key event listener, you'll want to return true if the Back key was pressed. This tells the Android OS that you're overriding the behavior of that key.


See our KeyEvents sample on GitHub for an example of this: https://github.com/coronalabs/samples-coronasdk/blob/master/Hardware/KeyEvents/main.lua#L72