Where do I purge a scene when leaving it?

Hello,

I have an app with a few scenes. One of them loads the webView with a web page which works fine. I then click to go to another scene. When I come back to the scene with the webView it is not there. I am adding the webView in the createScene event. But I am also adding the imgHeader and it is showing the second time when I return to the page. So am I supposed to purge this scene somewhere? Or what do you think I need to do to see the webView every time?

Thanks for your help!!

Warren

---------------------------------------------------------------------------------- -- -- SceneFacebookWeb.lua -- ---------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) storyboard.removeAll() local scene = storyboard.newScene() local widget = require ( "widget" ) local imgHeader local webView -- Touch event listener for background image --local function onSceneTouch( self, event ) local function onSceneTouch( event ) if event.phase == "began" then --MENU BAR if event.x \> 0 and event.y \> 90 and event.x \< 48 and event.y \< 121 then webView:removeSelf() storyboard.gotoScene( "SceneHome" ) end if event.x \> 47 and event.y \> 90 and event.x \< 100 and event.y \< 121 then webView:removeSelf() storyboard.gotoScene( "SceneEvents" ) end if event.x \> 99 and event.y \> 90 and event.x \< 182 and event.y \< 121 then webView:removeSelf() storyboard.gotoScene( "SceneHappyHour" ) end if event.x \> 181 and event.y \> 90 and event.x \< 251 and event.y \< 121 then webView:removeSelf() storyboard.gotoScene( "SceneFacebook" ) end if event.x \> 250 and event.y \> 90 and event.x \< 320 and event.y \< 121 then webView:removeSelf() storyboard.gotoScene( "SceneLocation" ) end end return true end --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- Called when the scene's view does not exist: function scene:createScene( event ) local screenGroup = self.view imgHeader = display.newImage( "Header.jpg") screenGroup:insert( imgHeader ) imgHeader:addEventListener("touch", onSceneTouch) display.setDefault( "background", 0, 0, 0 ) webView = native.newWebView( 0, 123, 320, 357 ) webView:request( "http://www.facebook.com" ) group:insert( webView ) screenGroup:insert( imgHeader ) end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) --storyboard.purgeScene( "sceneFacebook" ) imgHeader.ready = true --[[-- Update Lua memory text display local showMem = function() imgHeader:addEventListener( "touch", imgHeader ) end local memTimer = timer.performWithDelay( 1000, showMem, 1 ) --]] end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view imgHeader.ready = false end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene

If you want to remove scene 1 when you navigate to scene 2, In scene 2’s createScene function try the following.

[lua]

function scene:createScene( event )
    local screenGroup = self.view

    – == If there was a previous screen like the help - totally remove it from memory
    if storyboard.getPrevious() ~= nil then
        --print("previous screen in mainmenu " …  storyboard.getPrevious());
        storyboard.purgeScene(storyboard.getPrevious())
        storyboard.removeScene(storyboard.getPrevious())
    end

end

[/lua]

Thanks a lot! That did the trick.

Warren

Glad I could help.

Larry

If you want to remove scene 1 when you navigate to scene 2, In scene 2’s createScene function try the following.

[lua]

function scene:createScene( event )
    local screenGroup = self.view

    – == If there was a previous screen like the help - totally remove it from memory
    if storyboard.getPrevious() ~= nil then
        --print("previous screen in mainmenu " …  storyboard.getPrevious());
        storyboard.purgeScene(storyboard.getPrevious())
        storyboard.removeScene(storyboard.getPrevious())
    end

end

[/lua]

Thanks a lot! That did the trick.

Warren

Glad I could help.

Larry

Hi doubleslashdesign,

I am curious as to why you remove the previous screen in the createScene handler? 

I tried your method in my existing code and I found that there was a noticeable “blink” during the transition. The previous scene is being removed, resulting in a blank screen then the new screen moves into view. Although it happens so fast I can still see a “blink”.

In my experience I found that placing that same code in the enterScene handler works better. The previous scene is removed after the new scene is in full view thus eliminating the “blink”.

Also I was wondering why you perform a purgeScene and a removeScene? Have you found any added benefit to doing both as opposed to just a removeScene (which automatically does both)?

Just curious - Thanks

I would agree with you eddierush. 

I remove my scenes in enterScene for the reason you mentioned, if you are transitioning then you get a black screen before everything has finished. 

You’re right about removeScene too, if you are using that then there is no reason to use purgeScene, as removeScene purges the scene anyway (as well as removing the lua objects).

Actually I never noticed a Blinking of my Scene Transisions, but you are both correct it would be better to put that code in the enterScene event handler instead of the createScene.

Larry

Hi doubleslashdesign,

I am curious as to why you remove the previous screen in the createScene handler? 

I tried your method in my existing code and I found that there was a noticeable “blink” during the transition. The previous scene is being removed, resulting in a blank screen then the new screen moves into view. Although it happens so fast I can still see a “blink”.

In my experience I found that placing that same code in the enterScene handler works better. The previous scene is removed after the new scene is in full view thus eliminating the “blink”.

Also I was wondering why you perform a purgeScene and a removeScene? Have you found any added benefit to doing both as opposed to just a removeScene (which automatically does both)?

Just curious - Thanks

I would agree with you eddierush. 

I remove my scenes in enterScene for the reason you mentioned, if you are transitioning then you get a black screen before everything has finished. 

You’re right about removeScene too, if you are using that then there is no reason to use purgeScene, as removeScene purges the scene anyway (as well as removing the lua objects).

Actually I never noticed a Blinking of my Scene Transisions, but you are both correct it would be better to put that code in the enterScene event handler instead of the createScene.

Larry