Runtime event listener not being removed

Sorry Jay, it’s true is this case  ;).

You are correct in that scene.foo = “bar” is visible within the whole module, but that’s because scene is declared local at the top of the module making it visible to the whole module.

Runtime is a global object that’s visible from everywhere. The problem with the code above is that the *listener* is declared local inside another function, which means the reference to it will disappear as soon as the function ends. Runtime:removeEventListener() requires a reference to the listener that it should remove. Since that reference is gone (nil) when removeEventLister is called, it can’t remove the listener.

In other words, onAndroidKeyEvent is only visible in scene:create() in the code above.

I don’t want to question a jedi forum member :), but is that possibly true? I can do scene.foo = “bar” inside sceneCreate then scene.foo is available in every other function. Isn’t Runtime an “upper level” object just like scene (but even up higher)?

And when I do Runtime:addEventListener I’m not getting a handle back; I assumed Corona was taking care of that behind the scene, which is why you have to do a remove using the same parameters as the add.

When using Composer, how are you supposed to do Runtime:addEventListener outside of a function since everything runs based on functions?

 Jay

PS - I discovered this thread because I’m having problems with a Runtime event listener that won’t go away. :slight_smile:

Sorry Jay, it’s true is this case  ;).

You are correct in that scene.foo = “bar” is visible within the whole module, but that’s because scene is declared local at the top of the module making it visible to the whole module.

Runtime is a global object that’s visible from everywhere. The problem with the code above is that the *listener* is declared local inside another function, which means the reference to it will disappear as soon as the function ends. Runtime:removeEventListener() requires a reference to the listener that it should remove. Since that reference is gone (nil) when removeEventLister is called, it can’t remove the listener.

In other words, onAndroidKeyEvent is only visible in scene:create() in the code above.