My app crashes when hitting the back button

Thanks for the reply.

For the listeners I changed my code to this…

btnEasy:addEventListener("tap", easySetup) btnEasy:removeEventListener("tap", easySetup) local function easySetup() gotoNextScene(DIFF\_EASY) end

It still crashed.  :frowning:

A little more info on the app. After the “Menu” scene, the “Memory” scene loads then alternates between the “Memory” and “Guess” scenes.

This is a pretty simple app to get my feet wet. The only onComplete function I have is for a sound. Other then the back button, there are no Runtime events. I’m not using any physics or timers either.

I removed the sound, but it still crashes.

I’m checking the memory usage using collectgarbage(“count”) it reports the same amount every time, so I’m assuming things are cleaning up nicely.

For fun, I commented out the purge

function scene:enterScene(event) --storyboard.purgeScene("memory") --storyboard.purgeScene("guess") end

I wasn’t too hopeful about this since it’s something new I added; the crash has been around for a while. It still crashed  :frowning:

It may be time to put in print statements to try and narrow down where it’s happening.

Rob

I tried that today and put print statements EVERYWHERE, they were all hit.  :( So something is happening after the code is run.

I even tried stripping down the app to its most basic, see the complete code below. It still crashes.  :frowning:

main.lua

local storyboard = require("storyboard") local function onKeyEvent( event ) local keyName = event.keyName if (keyName == "back") then storyboard.gotoScene("scene1") end end storyboard.gotoScene("scene1") Runtime:addEventListener("key", onKeyEvent)

scene1.lua

local storyboard = require("storyboard") local scene = storyboard.newScene() function scene:createScene(event) local group = self.view local btnEasy local function gotoNextScene() local options = { effect = "fade", time = 400 } storyboard.gotoScene("scene2", options) return true end btnEasy = display.newText("Scene 1", 0, 0, "", 50) btnEasy:addEventListener("tap", gotoNextScene) btnEasy.x = 200 btnEasy.y = 200 group:insert(btnEasy) end scene:addEventListener("createScene", scene) return scene

scene2.lua

local storyboard = require("storyboard") local scene = storyboard.newScene() function scene:createScene(event) local group = self.view local btnEasy local function gotoNextScene() local options = { effect = "fade", time = 400 } storyboard.gotoScene("scene1", options) return true end btnEasy = display.newText("Scene 2", 0, 0, "", 50) btnEasy:addEventListener("tap", gotoNextScene) btnEasy.x = 200 btnEasy.y = 200 group:insert(btnEasy) end scene:addEventListener("createScene", scene) return scene

I also added the back button code to the “Storyboard” example project in the main.lua file, it still crashes.  :frowning:

local function onKeyEvent( event ) local keyName = event.keyName if (keyName == "back") then storyboard.gotoScene("scene1") end end Runtime:addEventListener("key", onKeyEvent)

Am I doing something stupid? Could there be a bug with Corona? Maybe I’ll try re-installing everything tomorrow.  :wacko:

can you try this:

local function onKeyEvent( event ) local keyName = event.keyName if (keyName == "back") then print( "Back pressed" ) storyboard.gotoScene("scene1") end return true end Runtime:addEventListener("key", onKeyEvent)

And tell me know many times “Back pressed” gets printed to the console?

I think I know where you’re going with this, I can only assure you I’m not double tapping lol. The “Memory” statement is coming from the next scene after the “menu” scene.

I still have the purge commented out, so it’s only the print statement.

function scene:enterScene(event) print("menu enterScene") --storyboard.purgeScene("memory") --storyboard.purgeScene("guess") end

This is a screen shot from the Emulator:

emulator.png

And from my test device (a tablet):

tablet.png

I know exactly where Danny is going.  There are two phases for the key press:  “up” and “down”.  If you don’t trap for that, you end up executing the code twice, once for “down” and once for “up”.

Rob

I don’t know where I stole my original code from, but I changed it to this:

local function onKeyEvent( event ) local keyName = event.keyName local phase = event.phase if (keyName == "back" and phase == "up") then print( "Back pressed" ) storyboard.gotoScene("menu") end end

emulator2.png

The app still crashes *cries*

I think I have it.  You’re not returning true or false back to the system to know if it’s handling the event or not.  A return of true means “Hey I handled that”.  A return of false means “hey OS you need to take care of this”.

local storyboard = require("storyboard") local function onKeyEvent( event )     local keyName = event.keyName     local phase = event.phase     if (keyName == "back" and phase == "up") then         print( "Back pressed" )         storyboard.gotoScene("scene1")         return true     end     return false end storyboard.gotoScene("scene1") Runtime:addEventListener("key", onKeyEvent)

The un-trapped up key was telling the OS to exit the app.  It really wasn’t crashing.

Rob

That was it. :lol:

Why is it always the simple solutions that cause the most grief? lol

Thanks so much for your help.  :slight_smile:

Hi guys, Very helpful post! Just a quick question: I am assuming that the onKey listener function need to be present ONLY in the main.lua and not on every scene right? (using Storyboard) Thanks a lot. I am starting converting an iOS app and the back button is giving a headache! Mo

That’s correct. Main.lua is where I have the “Best Answer” code. It’s global, so it doesn’t matter what scene your on when you click the back button, the listener function will be called.

You only need to catch the keys with one event listener. To me main.lua is the best place to do that.

Rob

Thanks Patrick, Rob. Yes I can see now why my app was behaving bizarre. I had the onKey listener on each scene!!

Cheers.

Mo

This one got me too. I had a onButtonPress(event) function but I did not have return true / return false in my code. It worked fine on IOS and worked fine on Android during initial testing but after repetitive button press I got the Android crash with lots of logcat gory but the one line that stood out was :

“dequeueBuffer failed (Invalid argument)” 

When I googled that line thankfully I was pointed to this thread which probably saved me a few days of anguish at the very least. Thanks much for posting it (OP) and finding the solution (Rob). 

EDIT : Just checkout the docs for widget.newButton. None of the examples show return true / false. Ref : http://docs.coronalabs.com/api/library/widget/newButton.html

I think, given the serious consequences this issue should be highlighted in all relevant API docs and samples should be updated to have return true / false in them on a consistent basis. Many of us simply copy/paste from these API pages so chance are there are lots others with this issue lurking in their code.

EDIT2 : Ok my crash is still there. More troubleshooting and then possibly a thread on its own. Oh well… 

The widget libraries button handlers, return true internally, so you don’t explicitly need to do so from your own code.

I believe the docs state that, don’t they? In the listener section?

Just explaining why the docs don’t show it being handled :slight_smile:

Thanks for this insight. I don’t believe I saw this in the docs but I might have easily missed it. I appreciate your chiming in here. My problem is elsewhere it appears but its too hard to try and pin it down. I’m just going to code around it.

This one got me too. I had a onButtonPress(event) function but I did not have return true / return false in my code. It worked fine on IOS and worked fine on Android during initial testing but after repetitive button press I got the Android crash with lots of logcat gory but the one line that stood out was :

“dequeueBuffer failed (Invalid argument)” 

When I googled that line thankfully I was pointed to this thread which probably saved me a few days of anguish at the very least. Thanks much for posting it (OP) and finding the solution (Rob). 

EDIT : Just checkout the docs for widget.newButton. None of the examples show return true / false. Ref : http://docs.coronalabs.com/api/library/widget/newButton.html

I think, given the serious consequences this issue should be highlighted in all relevant API docs and samples should be updated to have return true / false in them on a consistent basis. Many of us simply copy/paste from these API pages so chance are there are lots others with this issue lurking in their code.

EDIT2 : Ok my crash is still there. More troubleshooting and then possibly a thread on its own. Oh well… 

The widget libraries button handlers, return true internally, so you don’t explicitly need to do so from your own code.

I believe the docs state that, don’t they? In the listener section?

Just explaining why the docs don’t show it being handled :slight_smile:

Thanks for this insight. I don’t believe I saw this in the docs but I might have easily missed it. I appreciate your chiming in here. My problem is elsewhere it appears but its too hard to try and pin it down. I’m just going to code around it.