native.newTextField remains on scene

Hi,

After I go back to list_categories.lua scene from add_or_edit_category.lua scene native.newTextField remains on scene with list of categories. What is proper way remove/hide text field object from scene?

I use back button to go back to previous scene.

-- add_or_update_category.lua

local slm           = require 'lib.slm'
local tb            = require 'lib.touch_buttons'
local history       = require 'lib.history'
local sqlite3       = require 'sqlite3'
local composer      = require 'composer' 
local scene         = composer.newScene()

local current_scene_name  = composer.getSceneName( 'current' )
local previous_scene_name = 'scenes.list_of_categories'
history.addScene( current_scene_name, previous_scene_name )
 
function scene:create( event )
    local sceneGroup             = self.view
    local params                 = event.params or {}
    local category_name_for_edit = params.category_name or ''
    
    local text_input_category_name = native.newTextField( 0, 0, 500, 100 )
    text_input_category_name.text = category_name_for_edit
    sceneGroup:insert( text_input_category_name )
end
 
function scene:show( event )
 
    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
   
    elseif ( phase == "did" ) then
   
    end
end
 
function scene:hide( event )
 
    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
    
    elseif ( phase == "did" ) then
   
    end
end
 
function scene:destroy( event )

end
 
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
 
return scene

Have a nice day:)
ldurniat

Within the scene that you are going to hide, in the composer hide function you have to manually remove the textfield from the scene group, since it is a native component.

2 Likes

https://docs.coronalabs.com/api/library/native/newTextField.html#gotchas

To remove a native text field from the display, use object:removeSelf() .

https://docs.coronalabs.com/api/type/DisplayObject/removeSelf.html

May be just make it invisible instead of recreating?

You can try to set isVisible to false and see if it hides, but I don’t think it is a good practice because you will continue with a component spending memory on a screen that you will not be using the component. But you decide that.