Cannot run same scene twice without restart of app

Hi,

In this scene I try to get users to submit location of intresting places. That works. But if they do it twice, without restart the app, there is no GPS information on the second submission. 

I am very new to this, so the code is a bit of a mess, sorry about that. I guess I have placed the GPS function in the wrong part of the scene. If anyone could see what I should change I would be very thankful. 

Thanks !

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen -- -- local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 ) local background = display.newImageRect( sceneGroup, "background.PNG", display.contentWidth, display.contentHeight ) background.x = display.contentCenterX background.y = display.contentCenterY local titletext = display.newText( sceneGroup, "- Create POI -", 300, 80, native.systemFont, 60) titletext.x = display.contentCenterX titletext.y = 80 titletext:setFillColor(0, 1, 0) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- &nbsp; &nbsp; DISPLAY GPS LOCATION -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local latitude = display.newText( "-", 100, 50, native.systemFont, 16 ) local longitude = display.newText( "-", 100, 100, native.systemFont, 16 ) local altitude = display.newText( "-", 100, 150, native.systemFont, 16 ) local speed = display.newText( "-", 100, 250, native.systemFont, 16 ) local locationHandler = function( event ) &nbsp; &nbsp; -- Check for error (user may have turned off location services) &nbsp; &nbsp; if ( event.errorCode ) then &nbsp; &nbsp; &nbsp; &nbsp; native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} ) &nbsp; &nbsp; &nbsp; &nbsp; print( "Location error: " .. tostring( event.errorMessage ) ) &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; local latitudeText = string.format( '%.4f', event.latitude ) &nbsp; &nbsp; &nbsp; &nbsp; latitude.text = latitudeText &nbsp; &nbsp; &nbsp; &nbsp; local longitudeText = string.format( '%.4f', event.longitude ) &nbsp; &nbsp; &nbsp; &nbsp; longitude.text = longitudeText &nbsp; &nbsp; &nbsp; &nbsp; local altitudeText = string.format( '%.3f', event.altitude ) &nbsp; &nbsp; &nbsp; &nbsp; altitude.text = altitudeText &nbsp; &nbsp; &nbsp; &nbsp; local speedText = string.format( '%.3f', event.speed ) &nbsp; &nbsp; &nbsp; &nbsp;speed.text = speedText &nbsp; &nbsp; end end -- Get the table of current values for all columns -- This can be performed on a button tap, timer execution, or other event -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- &nbsp; &nbsp; COMMENT TEXTBOX -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local defaultBox local function textListener( event ) &nbsp; &nbsp; if ( event.phase == "began" ) then &nbsp; &nbsp; &nbsp; &nbsp; -- User begins editing "defaultBox" &nbsp; &nbsp; elseif ( event.phase == "ended" or event.phase == "submitted" ) then &nbsp; &nbsp; &nbsp; &nbsp; -- Output resulting text from "defaultBox" &nbsp; &nbsp; &nbsp; &nbsp; print( event.target.text ) &nbsp; &nbsp; elseif ( event.phase == "editing" ) then &nbsp; &nbsp; &nbsp; &nbsp; print( event.newCharacters ) &nbsp; &nbsp; &nbsp; &nbsp; print( event.oldText ) &nbsp; &nbsp; &nbsp; &nbsp; print( event.startPosition ) &nbsp; &nbsp; &nbsp; &nbsp; print( event.text ) &nbsp; &nbsp; end end -- Create text box defaultBox = native.newTextBox( 200, 200, 450, 250 ) defaultBox.text = "Add comment here " defaultBox.isEditable = true defaultBox.x = display.contentCenterX defaultBox.y = 400 defaultBox.font = native.newFont( native.systemFontBold, 30 ) -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- &nbsp; &nbsp; &nbsp;SEND REPORT BUTTON -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local widget = require( "widget" ) -- Function to handle button events local function handleSettingsButtonEvent( event ) &nbsp; &nbsp;if ( "ended" == event.phase ) then &nbsp; &nbsp; &nbsp; &nbsp;print( "Send Report Button was pressed" ) local json = require( "json" ) local mime = require( "mime" ) local function networkListener( event ) &nbsp; &nbsp; if ( event.isError ) then &nbsp; &nbsp; &nbsp; &nbsp; print( "Network error: ", event.response ) &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; print ( "RESPONSE: " .. event.response ) &nbsp; &nbsp; end end local body = {} body.resource = {} body.resource.gpslat = latitude.text body.resource.gpslon = longitude.text body.resource.gpsspeed = speed.text body.resource.gpsalt = altitude.text body.resource.alliance = system.getPreference( "app", "alliance", "string" ) body.resource.nic = system.getPreference( "app", "nic", "string" ) body.resource.comment = defaultBox.text body = json.encode(body) local headers = {} headers["X-DreamFactory-Api-Key"] = "\<key here\>" headers["Authorization"] = "Basic \<ABC\>" headers["Content-Type"] = "application/json" local params = {} params.headers = headers params.body = body &nbsp;network.request( "http://server.domain/api/v2/cstri-mysql01/\_table/poi", "POST", networkListener, params ) composer.gotoScene( "menu" ) &nbsp; &nbsp;end end -- Create the widget local settingsbutton = widget.newButton( &nbsp; &nbsp;{ label = "button", &nbsp; &nbsp; &nbsp; &nbsp;onEvent = handleSettingsButtonEvent, &nbsp; &nbsp; &nbsp; &nbsp;emboss = false, &nbsp; &nbsp; &nbsp; &nbsp;-- Properties for a rounded rectangle button &nbsp; &nbsp; &nbsp; &nbsp;shape = "roundedRect", &nbsp; &nbsp; &nbsp; &nbsp;width = 600, &nbsp; &nbsp; &nbsp; &nbsp;height = 60, &nbsp; &nbsp; &nbsp; &nbsp;cornerRadius = 2, fillColor = { default={0, 0.50196078431373, 0, 1}, over={1,0.1,0.7,0.4} }, &nbsp; &nbsp; &nbsp; &nbsp;strokeColor = { default={0, 1, 0, 10}, over={0.8,0.8,1,1} }, labelColor = { default={ 0, 0, 0 }, over={ 1, 1, 0.87843137254902, 0.5 } }, &nbsp; &nbsp; &nbsp; &nbsp;strokeWidth = 4 &nbsp; &nbsp;} ) -- Center the button settingsbutton.x = display.contentCenterX settingsbutton.y = 700 -- Change the button's label text settingsbutton:setLabel( "Submit and return to menu" ) settingsbutton.\_view.\_label.size = 40 -- Adding the button to scene group sceneGroup:insert(settingsbutton) -- Activate location listener Runtime:addEventListener( "location", locationHandler ) end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

Can a solution be to add the following code on the main menu page? Maybe that can trigger a total re-load of the scene each time. 

composer.removeScene( "scenes.sendpoi" )

Thanks

Hi @anders61,

If you want to “kill” a scene so that it’s re-created each time your code redirects there, then yes, removing that scene (composer.removeScene()) is a valid way to do it.

Best regards,

Brent

I tried to put 

composer.removeScene( "scenes.sendpoi" )

 both on the page I leave and the main menu page, but still have the same problem, no GPS information second time I enter the sendpoi scene, if I dont restart the app.

Hi @anders61,

Do you get any response upon the scene being destroyed? To test, please add a print() statement or something to that phase block of the scene you’re destroying, for example:

[lua]

– destroy()

function scene:destroy( event )

    print( “SCENE DESTROYED!!!” )

end

[/lua]

@anders, why do you abstract the GPS code into a separate module and call that from an event (say a button press) in your scene?

That way you separate your code and it means you can use the GPS code from multiple scenes if required and/or call it repetitively from the same scene.

Say you create a module called myGPS and that exposed a function called submitLocation() you could call it like this

myGPS.submitLocation(\<params need to make network call\>)

Can a solution be to add the following code on the main menu page? Maybe that can trigger a total re-load of the scene each time. 

composer.removeScene( "scenes.sendpoi" )

Thanks

Hi @anders61,

If you want to “kill” a scene so that it’s re-created each time your code redirects there, then yes, removing that scene (composer.removeScene()) is a valid way to do it.

Best regards,

Brent

I tried to put 

composer.removeScene( "scenes.sendpoi" )

 both on the page I leave and the main menu page, but still have the same problem, no GPS information second time I enter the sendpoi scene, if I dont restart the app.

Hi @anders61,

Do you get any response upon the scene being destroyed? To test, please add a print() statement or something to that phase block of the scene you’re destroying, for example:

[lua]

– destroy()

function scene:destroy( event )

    print( “SCENE DESTROYED!!!” )

end

[/lua]

@anders, why do you abstract the GPS code into a separate module and call that from an event (say a button press) in your scene?

That way you separate your code and it means you can use the GPS code from multiple scenes if required and/or call it repetitively from the same scene.

Say you create a module called myGPS and that exposed a function called submitLocation() you could call it like this

myGPS.submitLocation(\<params need to make network call\>)