Android Back Button Problems

Hi all,

I’ve been working on this for two days now with no luck. I’ve tried everything I can think of. I want the back button on Android to go to the menu scene if the app is not on the menu scene. If the app is already on the menu scene, I want the back button to exit the app.

I have tried placing the code in the main.lua file but the back button did not respond to my taps. I tried putting the code in each scene and removing the event listener upon scene exit but the app will always exit. If I am in a scene that is not the menu, the back button will make the app go to the menu scene then immediately exit.

Here is the code I have been using.

local function onKeyEvent(event) local phase = event.phase local keyName = event.keyName print( event.phase, event.keyName ) if ( "back" == keyName and phase == "up" ) then if storyboard.currentScene == "scene\_home" then native.requestExit() return true else storyboard.gotoScene( "scene\_home" ) return true end return false end end Runtime:addEventListener( "key", onKeyEvent )

Can someone please explain to me why this always exits the app? I would greatly appreciate it.

Eric

The “return false” needs to be outside the if statement, but I doubt that’s your issue.

I would put in some print statements and see what’s happening. You can use “adb logcat” to watch the devices console.log.  If you don’t know how to do that, this tutorial will guide you.

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Rob

Hi Rob,

I just went through and tested it using adb logcat and it runs through my else statement and goes to the file “scene_home”. It also looks as though there are 4 instances of the button press happening but I think there should only be one. Is this because it is detecting it so quickly? After going to the “scene_home” file the first time, it runs through and exits the app on the rest of the instances. Is there something I am missing? Or perhaps there is a better way to implement this?

Eric

I would expect two passes, one for key DOWN and one for key UP, but you seem to be trapping for that.  If it’s indeed firing multiple times (two Down, two up), then your code is working. The first time it goes to the menu, the next it it exits.  It sounds to me like you may have the key event being added multiple times.  Do you have this main.lua or in your scenes?

It is currently in my scenes. When I tried it in my main.lua, nothing happened. Would this 

storyboard.removeAll()

affect it at all? I have this at the beginning of each scene to remove the previous scene.

No.  The listener is tied to the Runtime so removing the scene doesn’t do anything. You would have to explicitly remove it.  I think you are better off getting this working from main.lua actually.

Rob

Ah! I found the problem using adb logcat. In my main.lua file, I was requiring storyboard after my code so the storyboard.currentScene line was not working. I moved the line where I require storyboard above the code and it works perfectly now.

Thanks a ton, Rob!

Eric

Thanks for posting the question and your ultimate solution. It helps a lot as I just started tackling the Back button. I have a quick question… Do you detect if the app is running on an Android and only then setup the Back button listener or is ok to keep this code segment in your IOS app even though you know there will never be a back button event. Any harm leaving it in? Thanks for your help.

Hi ksan,

To my knowledge you can leave it in. I do not detect if it is running on android and I tested the app on an iPad today and it worked fine with this code in there. I’m not sure if Apple will know or care. I will give you an update when I submit the app (soon) about whether the code can be left in.

If someone else knows this information already, it would be great to hear.

Eric

iOS devices are very unlikely to ever generate that event.  I don’t check the OS before I enable back key support since it just sits there harmless on iOS, but if you wanted to be 100% safe from anything that might happen in the future, you could put the Runtime:addEventListener() call inside of an if statement that tested to see if you were on an Android platform or not.

It will be interesting to see  how this is addressed on Windows platforms.  I don’t know anything about their setup.  Do Windows 8 phones have a back key?  What about the Surface?

Thank you very much for the feedback. Will be interesting to see indeed. 

The “return false” needs to be outside the if statement, but I doubt that’s your issue.

I would put in some print statements and see what’s happening. You can use “adb logcat” to watch the devices console.log.  If you don’t know how to do that, this tutorial will guide you.

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Rob

Hi Rob,

I just went through and tested it using adb logcat and it runs through my else statement and goes to the file “scene_home”. It also looks as though there are 4 instances of the button press happening but I think there should only be one. Is this because it is detecting it so quickly? After going to the “scene_home” file the first time, it runs through and exits the app on the rest of the instances. Is there something I am missing? Or perhaps there is a better way to implement this?

Eric

I would expect two passes, one for key DOWN and one for key UP, but you seem to be trapping for that.  If it’s indeed firing multiple times (two Down, two up), then your code is working. The first time it goes to the menu, the next it it exits.  It sounds to me like you may have the key event being added multiple times.  Do you have this main.lua or in your scenes?

It is currently in my scenes. When I tried it in my main.lua, nothing happened. Would this 

storyboard.removeAll()

affect it at all? I have this at the beginning of each scene to remove the previous scene.

No.  The listener is tied to the Runtime so removing the scene doesn’t do anything. You would have to explicitly remove it.  I think you are better off getting this working from main.lua actually.

Rob

Ah! I found the problem using adb logcat. In my main.lua file, I was requiring storyboard after my code so the storyboard.currentScene line was not working. I moved the line where I require storyboard above the code and it works perfectly now.

Thanks a ton, Rob!

Eric

Thanks for posting the question and your ultimate solution. It helps a lot as I just started tackling the Back button. I have a quick question… Do you detect if the app is running on an Android and only then setup the Back button listener or is ok to keep this code segment in your IOS app even though you know there will never be a back button event. Any harm leaving it in? Thanks for your help.

Hi ksan,

To my knowledge you can leave it in. I do not detect if it is running on android and I tested the app on an iPad today and it worked fine with this code in there. I’m not sure if Apple will know or care. I will give you an update when I submit the app (soon) about whether the code can be left in.

If someone else knows this information already, it would be great to hear.

Eric

iOS devices are very unlikely to ever generate that event.  I don’t check the OS before I enable back key support since it just sits there harmless on iOS, but if you wanted to be 100% safe from anything that might happen in the future, you could put the Runtime:addEventListener() call inside of an if statement that tested to see if you were on an Android platform or not.

It will be interesting to see  how this is addressed on Windows platforms.  I don’t know anything about their setup.  Do Windows 8 phones have a back key?  What about the Surface?