Device back button crashes app

Hi,

First off, I’m a Corona SDK beginner (just started 2 weeks ago) and I love how easy (except my problem :P) everything works! I’ve looked on the forum for post of people with similar but the ones I found couldn’t help me. I want to use device back button to control navigating through the scenes of my app. I’ve had a lot of help understanding how to tackle this issue using this post on the blog, but it still couldn’t help me.

Problem: whenever I press the back button on my device (HTC M8 mini) it crashes the app, returning it to my phone’s home screen with the message ‘testApp has stopped’. Although it does seem to work when pressing the back button in the main menu. 

This is my scene/menu setup:

main - menu - vegScreen - productScreen

I use the following code in my main.lua:

function onDeviceDown( event ) local phase = event.phase local keyName = event.keyName if("back" == keyName and phase == "up") then if (storyboard.currentScene == "menu") then native.requestExit() else local lastScene = storyboard.returnTo if (lastScene) then storyboard.gotoScene(lastScene) return true else native.requestExit() end end end return false end Runtime:addEventListener("key", onDeviceDown)

In every lua file I set the destination of the back button using storyboard.returnTo = “menu” (or whatever screen/scene to return to) in the createScene function. With my current code, when I press the device’s back button in the productScreen or the vegScreen it results in my app crashing and returning the ‘testApp has stopped’ message. 

I used the print function to check if the returnTo changes, which it does so there must me something wrong with onDeviceDown function, right?

I suggest you debug on device.

You can use standard Android logcat or my logging module:

Daniel

Hi Daniel,

Thanks for your response! Unfortunately the ADB part in the tutorial didn’t work. The SDK manager wouldn’t open even after fixing several common issues.

Though I got the advanced logging to work, it didn’t help either. The app just shuts down when I press the back button without logging the error.

Then its not a Lua error, but an error in Java. I suggesest you try to find the correct adb drivers for your device. It must work with correct drivers.

Daniel

I found out what I’ve been doing wrong:

I’ve been using storyboard throughout my project, but the way of getting the currentScene is intended for composer. I’ve replaced everything from storyboard to composer and it stopped the app from crashing and made the  back button work!

Thanks anyway!

I suggest you debug on device.

You can use standard Android logcat or my logging module:

Daniel

Hi Daniel,

Thanks for your response! Unfortunately the ADB part in the tutorial didn’t work. The SDK manager wouldn’t open even after fixing several common issues.

Though I got the advanced logging to work, it didn’t help either. The app just shuts down when I press the back button without logging the error.

Then its not a Lua error, but an error in Java. I suggesest you try to find the correct adb drivers for your device. It must work with correct drivers.

Daniel

I found out what I’ve been doing wrong:

I’ve been using storyboard throughout my project, but the way of getting the currentScene is intended for composer. I’ve replaced everything from storyboard to composer and it stopped the app from crashing and made the  back button work!

Thanks anyway!