Where do I remove a webView when leaving a scene?

These scenes sure do drive me crazy! I am trapping the back key on a scene page that uses the webView. When I see the back key I remove the webView and go to another scene that works. When I return back to the same page the back button gives me an error. JHere is my code. Should I be removing it somewhere else?

Thanks

local function onKeyEvent( event ) print("onKeyEvent22", event.keyName, event.phase ) --if event.keyPhase == "up" then if event.keyName == "back" then if webView.canGoBack == true then webView:back() return true else webView:removeSelf() storyboard.gotoScene( "sceneHome" ) return true end end --end return false end

What error does it give you?

My guess is that you’re not removing the key event listener when you exit the scene.  Thus, when you go back to the scene, the original listener is still active and trying to access a now non-existent webView.

  • Andrew

Can you show me the code to remove the key event listener and where it should be put?

Thanks!

Take a look at http://docs.coronalabs.com/guide/events/detectEvents/index.html#removing-event-listeners to see how to remove an event listener.

If that is in fact the problem (which I suspect it is), you’d probably want to put it right after you remove the webView and before you call storyboard.gotoScene(“sceneHome”).

  • Andrew

What error does it give you?

My guess is that you’re not removing the key event listener when you exit the scene.  Thus, when you go back to the scene, the original listener is still active and trying to access a now non-existent webView.

  • Andrew

Can you show me the code to remove the key event listener and where it should be put?

Thanks!

Take a look at http://docs.coronalabs.com/guide/events/detectEvents/index.html#removing-event-listeners to see how to remove an event listener.

If that is in fact the problem (which I suspect it is), you’d probably want to put it right after you remove the webView and before you call storyboard.gotoScene(“sceneHome”).

  • Andrew