Removing "native" objects when using "storyboard"

I’m having a problem removing native objects from the screen when using storyboard to change scenes. All other objects (e.g. display objects) are removed from the screen, but the native text fields that I am using remain on the screen after the new scene has been loaded. I have inserted these objects in the screenGroup, but they still persist. 

Any suggestions?

Native objects CANNOT be inserted into displayGroups. Although you won’t get an error, the native object will remain separate from the group.

If you need to remove them on changing scene, you will most likely need to have a forward declaration at the top of the lua file, create the native object as and when you need it, and then delete it manually during the exitScene function.

Example:

--myScene.lua local myTextField createScene() myTextField = native.newTextField(0, 0, 200, 50) myTextField("userInput", myTextFieldListener) end exitScene() if myTextField then myTextField:removeSelf() myTextField = nil end --best to remove keyboard focus, in case keyboard is still on screen native.setKeyboardFocus(nil) end

Hi, this is very timely! I have a related question.

How about a native.showWebPopup() call? I use native.cancelWebPopup() when I’m done and the WebPopup disappears from view. How about memory though? Do I need to do anything else to release the memory used by this particular native object?

And native.showPopup(“mail”, options) … That one just goes away when the email is sent or cancelled. There is no call similar to native.cancelWebPopup() make it go away. Will the memory be cleared once the email popup goes away?

And finally how about things like native.setActivityIndicator( ) or even native.showAlert () ? once you send away the activity indicator or once the alert box is dismissed they all go away neatly right? I am not doing anything to clean up after them. Hope thats ok. 

Thanks for your help. Regards,

Kerem

Thank you so much AlanPlantPot! Very helpful. 

I’m still having an issue removing text boxes from my screen during a scene change. I am able to remove the text field entitled “address” but not the one entitled “comments.”

[lua]

function scene:enterScene(event)

    --=========Address Field=================

    address = native.newTextField(135, 10, display.contentWidth - 145, 24)

    address.isEditable = true

    

    if address_store ~= nil then

        address.text = address_store

    end

    

    --=========Comment box==================

    comments = native.newTextField(10, display.contentHeight - 55, display.contentWidth - 20, 45)

    comments.isEditable = true

    

    if comments_store ~= nil then

        comments.text = comments_store

    end

    inspect_button:addEventListener(“tap”, chooseInspect)

    action_button:addEventListener(“tap”, chooseAction)

    tag_button:addEventListener(“tap”, chooseTag)

    

    picButton:addEventListener( “tap”, takePic )

end


function scene:exitScene(event)

    address_store = address.text

    address:removeSelf()

    address = nil

    

    comments_store = comments.text

    comments:removeSelf()

    comments = nil

    

    native.setKeyboardFocus(nil)

    inspect_button:removeEventListener(“tap”, chooseInspect)

    action_button:removeEventListener(“tap”, chooseAction)

    tag_button:removeEventListener(“tap”, chooseTag)

    

    picButton:removeEventListener( “tap”, takePic )

end

[/lua]

The problem is not removing “Text” only

The problem is "“ROMOVING ANY IMAGE ALSO”

I have this problem of removing a .png image on my scene and NO ONE KNOWS HOW TO DO IT.

I will keep trying to find out the solution, UNLESS IS A PROGRAM GLITCH OR A BUG THAT HAS NO SOLUTION.

But IF I CAN FIND THE SOLUTION I WILL POST A BIG VIDEO IN YOUTUBE TO HELP ALL THE PEOPLE WITH THE SAME PROBLEM THAN ME AND ANOTHER 40 THOUSANDS MORE.

Victor, please do not post in ALL CAPS.  It’s considered shouting and is not permitted on the forums.

Btw, solved the problem. I had forgotten to delete my text box declaration elsewhere in the program. 

Hi Rob.

I am sorry, I did not know that – thank you for letting me know.

Remember that I’m learning, I will not use all caps again.

Victor

@helloworld2013d You’ll have to give us some more info about the problem you are having, ideally post your code and someone will be able to see where you are going wrong. There’s certainly no general Corona bug when removing any .png image otherwise myself and many others would never have been able to create any apps. My initial guess is that you may have a scope issue. If you create a local variable in one function (e.g. the createScene function), you cannot remove it in another function(e.g. the exitScene function).

this is my complete code Alan


– --==***************************************************************************+±- –
– Music Theory Game
– By Victor M. Barba
– Copyright 2013 – All Rights Reserved

– Version 1.0
– --==***************************************************************************+±- –

local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()
local wrongSound = audio.loadSound( “wrongSound.mp3”)

– --==******************[FUNCTIONS TO GO TO ANOTHER SCENE]**********************+±- –

local function buttonHome()
        storyboard.gotoScene( “home”, “crossFade”, 1000 )    
    return true
end

local function buttonApple()
        storyboard.gotoScene( “learn1”, “crossFade”, 1000 )    
    return true
end

local function buttonBulbHome()
        storyboard.gotoScene( “ending”, “crossFade”, 1000 )    
    return true
end

local function buttonRound1()
        storyboard.gotoScene( “wrong1”, “crossFade”, 1000 )
        audio.play(wrongSound)    
    return true
end

local function buttonRound2()
        storyboard.gotoScene( “wrong1”, “crossFade”, 1000 )
        audio.play(wrongSound)
    return true
end

local function buttonRound3()
        storyboard.gotoScene( “level1Q6”, “crossFade”, 1000 )    
    return true
end

local function buttonRound4()
        storyboard.gotoScene( “wrong1”, “crossFade”, 1000 )
        audio.play(wrongSound)
    return true
end

local function buttonRound5()
        storyboard.gotoScene( “wrong1”, “crossFade”, 1000 )
        audio.play(wrongSound)
    return true
end

– --==**************************[CREATE SCENE]**********************************+±- –

function scene:createScene( event )
    local group = self.view
    
    local background = display.newImage( “backgroundLevel1Q5.png” )

    buttonHome = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = buttonHome
    }
    buttonHome.x = 522
    buttonHome.y = 655
    
    buttonApple = widget.newButton{
        defaultFile=“buttonApple.png”,
        onRelease = buttonApple
    }
    buttonApple.x = 420
    buttonApple.y = 710
    
    buttonBulbHome = widget.newButton{
        defaultFile=“buttonBulbHomeRelease.png”,
        overFile=“buttonBulbHomeOver.png”,
        onRelease = buttonBulbHome
    }
    buttonBulbHome.x = 615
    buttonBulbHome.y = 685
    
    buttonRound1 = widget.newButton{
        defaultFile=“button2Counts.png”,
        onRelease = buttonRound1
    }
    buttonRound1.x = 199
    buttonRound1.y = 312
    
    buttonRound2 = widget.newButton{
        defaultFile=“button1Count.png”,
        onRelease = buttonRound2
    }
    buttonRound2.x = 700
    buttonRound2.y = 432
    
    buttonRound3 = widget.newButton{
        defaultFile=“button4Counts.png”,
        onRelease = buttonRound3
    }
    buttonRound3.x = 800
    buttonRound3.y = 300
    
    buttonRound4 = widget.newButton{
        defaultFile=“button3Counts.png”,
        onRelease = buttonRound4
    }
    buttonRound4.x = 200
    buttonRound4.y = 600
    
    buttonRound5 = widget.newButton{
        defaultFile=“buttonHalfCount.png”,
        onRelease = buttonRound5
    }
    buttonRound5.x = 323
    buttonRound5.y = 444
---------------------------------------------------------------------insert into group----
    
    group:insert ( background )
    group:insert ( buttonHome )
    group:insert ( buttonApple )
    group:insert ( buttonBulbHome )
    group:insert ( buttonRound1 )
    group:insert ( buttonRound2 )
    group:insert ( buttonRound3 )
    group:insert ( buttonRound4 )
    group:insert ( buttonRound5 )
    
end

– --==***************************[ENTER SCENE]**********************************+±- –

function scene:enterScene( event )
    local group = self.view
    
    local quarterNote = display.newImage( “wholeNote.png” )
    quarterNote.x = 390
    quarterNote.y = 532
    transition.to(quarterNote, {x=987 , y=532, time=5000})
    
    group:insert ( quarterNote )
end

– --==***************************[EXIT SCENE]**********************************+±- –

function scene:exitScene( event )
    local group = self.view
    
    
end

– --==**************************[DESTROY SCENE]*********************************+±- –

function scene:destroyScene( event )
    local group = self.view    
        if buttonHome then
            buttonHome:removeSelf()
            buttonHome = nil
        end
        
        if buttonApple then
            buttonApple:removeSelf()
            buttonApple = nil
        end
        
        if buttonBulbHome then
            buttonBulbHome:removeSelf()
            buttonBulbHome = nil
        end
        
        if buttonRound1 then
            buttonRound1:removeSelf()
            buttonRound1 = nil
        end
        
        if buttonRound2 then
            buttonRound2:removeSelf()
            buttonRound2 = nil
        end
        
        if buttonRound3 then
            buttonRound3:removeSelf()
            buttonRound3 = nil
        end
        
        if buttonRound4 then
            buttonRound4:removeSelf()
            buttonRound4 = nil
        end
        
        if buttonRound5 then
            buttonRound5:removeSelf()
            buttonRound5 = nil
        end
end

– --==*************************[EVENT LISTENER]*********************************+±- –

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene


in the enterScene I have the image display. and this scene is – level 5 or level5.lua

you can see the transition.to point A to point B without any problems and everything works fine.

so I keep playing and going to another level level 6, level 7, level 8 but when I come back to level 5 …

As soon as I see the scene, the image (quarter note) is there already in point B

and a new image quarter note start to move and going from point A to point B, and you can see two images.

I would like the scene to start normal like the first time, every time.

I hope this will help you understand my problem. And I hope that you can help me.

Thank you Alan.

Victor

Native objects CANNOT be inserted into displayGroups. Although you won’t get an error, the native object will remain separate from the group.

If you need to remove them on changing scene, you will most likely need to have a forward declaration at the top of the lua file, create the native object as and when you need it, and then delete it manually during the exitScene function.

Example:

--myScene.lua local myTextField createScene() myTextField = native.newTextField(0, 0, 200, 50) myTextField("userInput", myTextFieldListener) end exitScene() if myTextField then myTextField:removeSelf() myTextField = nil end --best to remove keyboard focus, in case keyboard is still on screen native.setKeyboardFocus(nil) end

Hi, this is very timely! I have a related question.

How about a native.showWebPopup() call? I use native.cancelWebPopup() when I’m done and the WebPopup disappears from view. How about memory though? Do I need to do anything else to release the memory used by this particular native object?

And native.showPopup(“mail”, options) … That one just goes away when the email is sent or cancelled. There is no call similar to native.cancelWebPopup() make it go away. Will the memory be cleared once the email popup goes away?

And finally how about things like native.setActivityIndicator( ) or even native.showAlert () ? once you send away the activity indicator or once the alert box is dismissed they all go away neatly right? I am not doing anything to clean up after them. Hope thats ok. 

Thanks for your help. Regards,

Kerem

Thank you so much AlanPlantPot! Very helpful. 

I’m still having an issue removing text boxes from my screen during a scene change. I am able to remove the text field entitled “address” but not the one entitled “comments.”

[lua]

function scene:enterScene(event)

    --=========Address Field=================

    address = native.newTextField(135, 10, display.contentWidth - 145, 24)

    address.isEditable = true

    

    if address_store ~= nil then

        address.text = address_store

    end

    

    --=========Comment box==================

    comments = native.newTextField(10, display.contentHeight - 55, display.contentWidth - 20, 45)

    comments.isEditable = true

    

    if comments_store ~= nil then

        comments.text = comments_store

    end

    inspect_button:addEventListener(“tap”, chooseInspect)

    action_button:addEventListener(“tap”, chooseAction)

    tag_button:addEventListener(“tap”, chooseTag)

    

    picButton:addEventListener( “tap”, takePic )

end


function scene:exitScene(event)

    address_store = address.text

    address:removeSelf()

    address = nil

    

    comments_store = comments.text

    comments:removeSelf()

    comments = nil

    

    native.setKeyboardFocus(nil)

    inspect_button:removeEventListener(“tap”, chooseInspect)

    action_button:removeEventListener(“tap”, chooseAction)

    tag_button:removeEventListener(“tap”, chooseTag)

    

    picButton:removeEventListener( “tap”, takePic )

end

[/lua]

The problem is not removing “Text” only

The problem is "“ROMOVING ANY IMAGE ALSO”

I have this problem of removing a .png image on my scene and NO ONE KNOWS HOW TO DO IT.

I will keep trying to find out the solution, UNLESS IS A PROGRAM GLITCH OR A BUG THAT HAS NO SOLUTION.

But IF I CAN FIND THE SOLUTION I WILL POST A BIG VIDEO IN YOUTUBE TO HELP ALL THE PEOPLE WITH THE SAME PROBLEM THAN ME AND ANOTHER 40 THOUSANDS MORE.

Victor, please do not post in ALL CAPS.  It’s considered shouting and is not permitted on the forums.

Btw, solved the problem. I had forgotten to delete my text box declaration elsewhere in the program. 

Hi Rob.

I am sorry, I did not know that – thank you for letting me know.

Remember that I’m learning, I will not use all caps again.

Victor

@helloworld2013d You’ll have to give us some more info about the problem you are having, ideally post your code and someone will be able to see where you are going wrong. There’s certainly no general Corona bug when removing any .png image otherwise myself and many others would never have been able to create any apps. My initial guess is that you may have a scope issue. If you create a local variable in one function (e.g. the createScene function), you cannot remove it in another function(e.g. the exitScene function).