text field not removed when returning to calling scene

I am having trouble with input text fields not being removed from a called scene when I exit (call previous) then scene to go back to the calling scene. I have tried removing all of the scene objects from the display group, using composer.removeScene, etc and no success.

This is the only screen so far with input the scenes above are all simple button selection. I have the code listing attached for the two scenes.

I know it has to be something simple because it always is, it is just hiding from me at this moment :smiley: .

Any help will be appreciated.

[lua]


– CALLING SCENE

local composer = require(‘composer’) – Scene management
local scene = composer.newScene()
local widget = require (‘widget’) – Buttons
–local relayout = require(‘libs.relayout’) – Repositions elements on screen on window resize
local displayX = display.contentCenterX
local displayY = display.contentCenterY

  local function onProfileTouch( event )
    print(“in calling profile”)
    if “began” == event.phase then
      print(“character sub-menu: heading to profile screen”)
      composer.gotoScene(“scene.inputScreenTest”, “fade”, 350 )
      return true
    end
  end
 
 
function scene:create()
    --local _W, _H, _CX, _CY = relayout._W, relayout._H, relayout._CX, relayout._CY
    print(“character sub-menu:”)    
    local sceneGroup = self.view
    
    --relayout.add(characterBackground)
    
    local userNameLabel = display.newText("Profile ", displayX - 500, displayY -120, native.systemFont, 72)
          userNameLabel:setFillColor(1,1,1)
          userNameLabel.align = “left”
    sceneGroup:insert(userNameLabel)    


  – EVENT HANDLER FOR BACK BUTTON, THIS DOES GO BACK TO MAIN MENU
  – ISSUE ONCE BACK AT MAIN MENU, MAIN MENU DOES NOT RESPOND TO TOUCH EVENTS
-----------------------------------------------------------------------------------  
    – create a widget button to go to test profile scene
 
    local  profileButton = widget.newButton({
        width = 175,
        height = 100,
        x = displayX - 500,
        y = displayY -120,
        onEvent = onProfileTouch
    })
    sceneGroup:insert(profileButton)
    
end
– end scene create

function scene:show(event)
    local sceneGroup = self.view
    if “did” == event.phase then
      --composer.getSceneName(“previous”)

      print(“character:  show, did”, phase," no action assigned")
    elseif “will” == event.phase then
      if previousScene == nil then
— to resolve issue with most recent load of previousScene from submenu        
      end

    end
    
end

function scene:hide( event )
    local sceneGroup = self.view  
    local phase = event.phase
    if ( phase == “will” ) then
 -----------------------------------------------------------------------------------  
    elseif (phase == “did”) then
      print(“character:  hide”, phase," no action assigned")
    end
end   

– Called when scene is about to move offscreen:
function scene:exit( event )
    local sceneGroup = self.view
    – INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
    if event.phase == ‘will’ then
      
    end
end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroy( event )
    local sceneGroup = self.view

    if backButton then
        backButton:removeSelf()    – widgets must be manually removed
    end
end

– “create” event is dispatched if scene’s view does not exist
scene:addEventListener( “create” )

scene:addEventListener( “show”, scene)

scene:addEventListener( “hide”, scene )

– “exit” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exit” )

– “destroy” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to

scene:addEventListener( “destroy” )


return scene
 


– CALLED SCENE

local composer = require(‘composer’) – Scene management
local scene = composer.newScene()
local widget = require (‘widget’) – Buttons
–local relayout = require(‘libs.relayout’) – Repositions elements on screen on window resize
local displayX = display.contentCenterX
local displayY = display.contentCenterY
local userData = {}

  local function onExitTouch( event )
    print(“in backTouch previousScene is:”,previousScene)
    if “began” == event.phase then
      print(“in profile: heading back to menu”)
      composer.gotoScene(previousScene, “fade”, 900 )
      return true
    end
  end
 
  local function userNameListener( event )
      local userName   = nil

    if ( event.phase == “began” ) then
        – User begins editing “defaultField”
      print(‘user name’)
      
    elseif ( event.phase == “ended” or event.phase == “submitted” ) then
        – Output resulting text from “defaultField”
        print(event.name)
        userName = event.target.text
        table.insert(userData,1,userName)
        
        print( "target text: ",userName )

    elseif ( event.phase == “editing” ) then

    end
  end  
– end pre scene definitions

function scene:create()
    --local _W, _H, _CX, _CY = relayout._W, relayout._H, relayout._CX, relayout._CY
    print(“character sub-menu:”)    
    local sceneGroup = self.view
    
    – CALLED MENU SCENE FROM MAIN_FORUM SCENE

    local sampleBox = display.newRect(displayX,displayY, 400,100)
          sampleBox:setFillColor(.75,.75,.75)
    sceneGroup:insert(sampleBox)

    
    local userNameLabel = display.newText(“UserName”, displayX - 650, displayY -120, 400,80,native.systemFont, 72)
          userNameLabel:setFillColor(1,1,1)
          userNameLabel.align = “left”
    sceneGroup:insert(userNameLabel)
    
    local userName = native.newTextField(displayX-50,displayY-120, 500, 90)
          userName.font = native.newFont(native.systemFont, 42)
          userName:addEventListener( “userInput”, userNameListener )
    sceneGroup:insert(userName)
    
    local exitLabel = display.newText(“Exit”, displayX, displayY + 300,native.systemFont, 72)
          exitLabel:setFillColor(1,1,1)  
          exitLabel.align = “center”            
    sceneGroup:insert(exitLabel)  
    
    

    – create a widget button to return to main menu

  local  exitButton = widget.newButton({
        width = 300,
        height = 100,
        x = displayX ,
        y = displayY + 300,
        onEvent = onExitTouch
    })
    sceneGroup:insert(exitButton)
    
end
– end scene create

function scene:show(event)
    local sceneGroup = self.view
    if “did” == event.phase then
      print(“in profile:  show, did”, phase," no action assigned")
    elseif “will” == event.phase then
      previousScene=composer.getSceneName(“previous”)
      print(“in profile:  show, will previous is”, previousScene)
    end
    
end

function scene:hide( event )
    local sceneGroup = self.view  
    local phase = event.phase
    if ( phase == “will” ) then
      composer.removeScene(“scene.inputScreenTest”)  
      print(“in profile:  hide”, phase," cleanup")

      if exitButton then
        exitButton:removeSelf()
      end
----------------------------------------------  
    elseif (phase == “did”) then
      --composer.removeScene(“scenes.userProfile”)      
      print(“in profile:  hide”, phase," cleanup")
    end
end   

– Called when scene is about to move offscreen:
function scene:exit( event )
    local sceneGroup = self.view
    print(“in exit”)
    – INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
    if event.phase == ‘will’ then
      
    end
end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroy( event )
    local sceneGroup = self.view
    print(“in destroy”)

end

– “create” event is dispatched if scene’s view does not exist
scene:addEventListener( “create” )

scene:addEventListener( “show”, scene)

scene:addEventListener( “hide”, scene )

– “exit” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exit” )

– “destroy” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to

scene:addEventListener( “destroy” )


return scene
 

[/lua]

Hy!

I have had the same problem. I resolved it with 3 mods on the “CALLED SCENE”:

  1. the objects are created on “function scene:show( event )” AFTER “if ( phase == “did” ) then” and not on scene:create. Pay attention to set (one for all example) “local userName = native.newTextField(displayX-50,displayY-120, 500, 90)” only after the phase is DID.

  2. I declare a variable after the require section, at the top of the file, with my textfield “local userName = native.newTextField(displayX-50,displayY-120, 500, 90)” so the variable “userName” is usable in all the scene

  3. in “function scene:destroy( event )” I put “userName:removeSelf()”. In your case I think to put also in “function scene:hide( event )” after “if ( phase == “will” ) then” a object destroy “userName:removeSelf()”.

Hope to help you.

Text fields are not display objects. They sit on top of the Corona Display hierarchy.  What I’ve found to be the best practice is to delay creating the text fields until scene:show()'s “did” phase. If you’re transitioning the scene it looks weird for the fields to be there while the scene transitions. Then you manually remove them in scene:hide()'s “will” phase.

Because you may never call composer.removeScene() on your scene or it may be delayed until sometime into another scene, scene:destroy() may never be called or it may be very late, leaving your native object’s still on the screen. Create everything else in scene:create() but for native.* wait until the scene is on the screen before creating them.

Since scene:show() and scene:hide() always run in pairs, this will let you keep these native fields created at the time you need them.

Rob

Hm. I found this topic by searching, so experiencing kind of the same issue.

I’ve found, however, that if I transition between scenes with a simple { effect = “slideRight” } (or left…) everything works fine.

If I remove the effect parameter (for an instantaneous transition), or even leave { effect = “slideRight”, time = 1 } the problem persists.

So it seems somehow related to the speed at which the transition is performed (which is weird…)

FWIW, it is very inconvenient for me to leave the creation of native text fields for later, as I’m relaying on a library to create all screen elements.

With “slow” transitions, as said, everythiings seems to work correctly so far.

Best regards,

Jaime

The reason slideRight/Left works is because native.* objects inserted into a group will move with the group. Any transition that leaves the underlying scenes on top of each other won’t have an effect because native.* is always on top.

Humor me and set your transition speed to like 40 if you’re a 30fps app or 20 if you’re a 60fps app and let me know if that changes anything.

Rob

Well, with 

[lua]{ effect = “slideLeft”, time = 40 }[/lua]

(it’s at 30 fps), the behavior is fun, sometimes it works (that is, the native fields dissapear), sometimes they don’t and even this time when they stopped halfway:

Capture.PNG

:slight_smile:

… I should add, this is in the Windows simulator

Hy!

I have had the same problem. I resolved it with 3 mods on the “CALLED SCENE”:

  1. the objects are created on “function scene:show( event )” AFTER “if ( phase == “did” ) then” and not on scene:create. Pay attention to set (one for all example) “local userName = native.newTextField(displayX-50,displayY-120, 500, 90)” only after the phase is DID.

  2. I declare a variable after the require section, at the top of the file, with my textfield “local userName = native.newTextField(displayX-50,displayY-120, 500, 90)” so the variable “userName” is usable in all the scene

  3. in “function scene:destroy( event )” I put “userName:removeSelf()”. In your case I think to put also in “function scene:hide( event )” after “if ( phase == “will” ) then” a object destroy “userName:removeSelf()”.

Hope to help you.

Text fields are not display objects. They sit on top of the Corona Display hierarchy.  What I’ve found to be the best practice is to delay creating the text fields until scene:show()'s “did” phase. If you’re transitioning the scene it looks weird for the fields to be there while the scene transitions. Then you manually remove them in scene:hide()'s “will” phase.

Because you may never call composer.removeScene() on your scene or it may be delayed until sometime into another scene, scene:destroy() may never be called or it may be very late, leaving your native object’s still on the screen. Create everything else in scene:create() but for native.* wait until the scene is on the screen before creating them.

Since scene:show() and scene:hide() always run in pairs, this will let you keep these native fields created at the time you need them.

Rob

Hm. I found this topic by searching, so experiencing kind of the same issue.

I’ve found, however, that if I transition between scenes with a simple { effect = “slideRight” } (or left…) everything works fine.

If I remove the effect parameter (for an instantaneous transition), or even leave { effect = “slideRight”, time = 1 } the problem persists.

So it seems somehow related to the speed at which the transition is performed (which is weird…)

FWIW, it is very inconvenient for me to leave the creation of native text fields for later, as I’m relaying on a library to create all screen elements.

With “slow” transitions, as said, everythiings seems to work correctly so far.

Best regards,

Jaime

The reason slideRight/Left works is because native.* objects inserted into a group will move with the group. Any transition that leaves the underlying scenes on top of each other won’t have an effect because native.* is always on top.

Humor me and set your transition speed to like 40 if you’re a 30fps app or 20 if you’re a 60fps app and let me know if that changes anything.

Rob

Well, with 

[lua]{ effect = “slideLeft”, time = 40 }[/lua]

(it’s at 30 fps), the behavior is fun, sometimes it works (that is, the native fields dissapear), sometimes they don’t and even this time when they stopped halfway:

Capture.PNG

:slight_smile:

… I should add, this is in the Windows simulator