Event listner wont remove

In my createScene I have the following code
 

 local function onKeyEvent(event) local keyName = event.keyName local phase = event.phase local returnValue = false if(keyName == "back" and phase == "up") then local options = { effect = "slideRight", time = 500, } storyboard.gotoScene("start\_menu", options) returnValue = true end return returnValue end Runtime:addEventListener("key", onKeyEvent)

and in my exitScene I have the following code

function scene:exitScene(event) local group = self.view Runtime:removeEventListener("key", onKeyEvent) end

for some reason the exit scene gets called buy the listener never gets removed.

Dumb question but do you have the same event listener in the scene you are transitioning to?

Yes I do, is that a problem?

Are you sure the old listener isn’t removed?  Perhaps it is, and in your new scene you are re-creating the same listener

I’m not positive that it’s not removed I’m removing it the same exact way as above just in a different scene. In my code though I have a print statement after the back button is pressed and I print out what scene is calling it and it’s the current scene not the other one so I’m pretty sure the old scene listener is being removed. I hope that makes sense.

To be safe, try removing the whole event listener in the second scene and seeing if it still fires post-transition.

Okay remove it from which one? This is how my scnes go

main > start_menu > lobby

The issue I’m having is when I do the following

main > start_menu > lobby > start_menu > lobby > start_menu

Everytime I go back into the lobby it creates a new listener that wont get removed. So which one should I remove the listener from to test?

Actually I lied the main and start_menu don’t have the listener on it only the lobby does.

Is there a reason you’re not putting that in main.lua and leaving it?

Umm I suppose I could…never really thought about that. Is there an easy way to get what scene is loaded in corona other then setting it insde a var myself?

storyboard.getCurrentSceneName() see:

http://docs.coronalabs.com/api/library/storyboard/getCurrentSceneName.html

Unless you need the keyboard handler to do something different each scene (beyond specifying a different scene name to go back to), then it make sense to put it in each scene.  In programming there is a principle called the DRY principle.  Don’t repeat yourself.  if you have 30 scenes and you have to write that back handler 30 times, that’s a lot of work. 

What I would more likely do is put it all in main.lua and then in each scene, during the enterScene() function, set a variable that can be seen from main.lua (yea, it begs for a global, but wait!)   like adding a variable to the storyboard table called .goBackTo

So if your in level1.lua and the back button should take you back to menu.lua then do:

storyboard.goBackTo = “menu”

Then in your one function in main.lua you can say if the back button was hit, storyboard.gotoScene(storyboard.goBackTo)

Easy as pie.

Yeah that makes a lot of sense…I have a hard time with lua/corona with scope issues and trying to find code sometimes. I’ve developed a place all the code in 1 file per scene approach and if I need it more then once make it a class. It may not be the best approach but it’s helped me keep everything in place. I should rethink that approach though as I can see what you’re talking about.

P.S. moving it to the main.lua worked just fine

Dumb question but do you have the same event listener in the scene you are transitioning to?

Yes I do, is that a problem?

Are you sure the old listener isn’t removed?  Perhaps it is, and in your new scene you are re-creating the same listener

I’m not positive that it’s not removed I’m removing it the same exact way as above just in a different scene. In my code though I have a print statement after the back button is pressed and I print out what scene is calling it and it’s the current scene not the other one so I’m pretty sure the old scene listener is being removed. I hope that makes sense.

To be safe, try removing the whole event listener in the second scene and seeing if it still fires post-transition.

Okay remove it from which one? This is how my scnes go

main > start_menu > lobby

The issue I’m having is when I do the following

main > start_menu > lobby > start_menu > lobby > start_menu

Everytime I go back into the lobby it creates a new listener that wont get removed. So which one should I remove the listener from to test?

Actually I lied the main and start_menu don’t have the listener on it only the lobby does.

Is there a reason you’re not putting that in main.lua and leaving it?