Is there a glitch with widget.newSwitch?

For the last half hour I have been trying to create a switch that swaps between two scenes. I am trying to allow both scenes to have the same switch and am passing the state to a composer variable. The problem is when I print out the state of the switch it sometimes gives me the wrong boolean value. Is this just my implementation or is it a glitch?? Below is my code from view1 and view 2. 

P.S. It seems as though view1 the scene which is loaded at app start up does not register the correct boolean value at first. the print statement usually says “Switch with ID ‘onOffSwitch’ is on: false”

Another note both sections of code are in the scene create function and have sceneGroup:insert( onOffSwitch) at the end of the function. I just did not include this in the code snip its below 

View1.lua

local currentState = composer.getVariable( "switchState" ) local requestTableSwitchLabel = display.newText("Current Shuttle Requests", display.contentWidth-92,display.contentHeight-70, native.systemFont, 14 ) -- Handle press events for the checkbox local function onSwitchPress( event ) local switch = event.target print( "Switch with ID '"..switch.id.."' is on: "..tostring(switch.isOn) ) if tostring(switch.isOn)=="false" then print("STATE:"..tostring(switch.isOn)) composer.setVariable( "switchState", false ) composer.gotoScene( "view2" ) -- elseif tostring(switch.isOn)=="true" then -- -- onOffSwitch.initialSwitchState = currentState -- -- composer.gotoScene( "view1" ) end end -- Create the widget local onOffSwitch = widget.newSwitch( { left = display.contentWidth-138, top = display.contentHeight-55, style = "onOff", id = "onOffSwitch", initialSwitchState = currentState, label="Switch", onPress = onSwitchPress } )

View2.lua

local requestTableSwitchLabel = display.newText("Current Shuttle Requests", display.contentWidth-92,display.contentHeight-70, native.systemFont, 14 ) local currentState = composer.getVariable( "switchState" ) print("CURRENT STATE: "..tostring(currentState)) -- Handle press events for the checkbox local function onSwitchPress( event ) local switch = event.target print( "Switch with ID '"..switch.id.."' is on: "..tostring(switch.isOn) ) if tostring(switch.isOn)=="true" then composer.setVariable( "switchState", false) composer.gotoScene( "view1" ) end end -- Create the widget local onOffSwitch = widget.newSwitch( { left = display.contentWidth-138, top = display.contentHeight-55, style = "onOff", id = "onOffSwitch", initialSwitchState = currentState, label="Switch", onPress = onSwitchPress } )

I think the problem is you are using the onPress event vs. the onRelease event. The onPress event fires *before* the switch changes. This is likely why you think you’re getting the wrong state.  The onRelease event happens after the switch changes and will report the state you want.  

Give that a try!

Rob

I think the problem is you are using the onPress event vs. the onRelease event. The onPress event fires *before* the switch changes. This is likely why you think you’re getting the wrong state.  The onRelease event happens after the switch changes and will report the state you want.  

Give that a try!

Rob