Runtime "system" Event Listener Not Being Removed

Hi all,

I’m using the following method to fix a problem with my snapshot during an app resume:

P.onSystemEvent = function ( event ) if(event.type=="applicationResume") then P.powerGlow:invalidate() end end

I’m registering using the following call:

Runtime:addEventListener( "system", P.onSystemEvent )

And I’m removing with this call:

Runtime:removeEventListener( "system", P.onSystemEvent )

The event registers fine and my snapshot updates as it should when the app resumes.  Problem is onSystemEvent is still being fired even after I remove the event listener, obviously indicating a problem with my remove call. 

The remove call is returning nil, not true or false as Documentation says it should.

Check out the following debug:

local removeResult = Runtime:removeEventListener( "system", P.onSystemEvent ) print("P.onSystemEvent = " .. tostring(P.onSystemEvent)) print("removeResult = " .. tostring(removeResult))

Returns:

P.onSystemEvent = function: 10624940 removeResult = nil

Any ideas? 

removeEventListener should returns a boolean or nil, so that is expected.

Are you instantiating ‘P’ only once? Your results would indicate that the ‘P’ which you think you’re removing the listener from is not the ‘P’ which you actually are removing from.

Have you tried putting a print statement inside the listener function and, instead of removing the listener, simply print out something when the remove should happen? This might tell you how often the remove is being attempted and if the function is being called spuriously.

Thanks for the debug tip, @horace,

Turns out I was adding the system listener inside of a loop.  :huh:

Onward I go.

removeEventListener should returns a boolean or nil, so that is expected.

Are you instantiating ‘P’ only once? Your results would indicate that the ‘P’ which you think you’re removing the listener from is not the ‘P’ which you actually are removing from.

Have you tried putting a print statement inside the listener function and, instead of removing the listener, simply print out something when the remove should happen? This might tell you how often the remove is being attempted and if the function is being called spuriously.

Thanks for the debug tip, @horace,

Turns out I was adding the system listener inside of a loop.  :huh:

Onward I go.