Search code examples
luacoronasdkback-button

Back button does not navigate to required scene


On "tap", firstBar1.lua goes to scene1.lua. If there is no tap during the scene1 timeout then it moves to scene2 and so on; scene1 has a goBack button directed to firstBar1. It goes back for a moment but then continues to scene 2. It needs to stay on firstBar1 until there is another tap. I've tried various button codes and this is the best outcome I have been able to get. print ("firstBar1") doesn't work after tapping the back button.

    -- firstBar1.lua
    -----------------------------------------------------

    local composer = require ("composer")            
            print ("firstBar1")
    local scene = composer.newScene()                                       
    function scene:create(event) 
            local screenGroup = self.view

    local widget = require ("widget")

    local function onScene1BtnRelease()
            composer.gotoScene("scene1", "fade", 40)
            return true     
    end

    image1 = "images/staveBlankgrey2.png" 

    local scene1Btn = widget.newButton{
            defaultFile = image1,
            width = 480, height = 320,
            onRelease = onScene1BtnRelease
    } 
        screenGroup:insert(scene1Btn) 

    end

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

  return scene

Scene1:

    -- scene1.lua
    --------------------------------------------------------------------

  local mydata = require ("mydata")

    local composer = require ( "composer")
    local scene = composer.newScene()

            local function showScene2()
                    local options = {
                            effect = "slideLeft",
                            time = 130,
                    }
            composer.gotoScene("scene2", options)
            end

    -- create scene 3

    function scene:createScene ( event )
    local sceneGroup = self.view
    end     

  function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

    -- will

    if ( phase == "will") then

            data = data + 1
                    if data == 5 then
                    sub = sub + 1
                    data = 1
            end
            if sub == 3 then
            section = section + 1
                    sub = 1
                    data = 1
            end

        stave = display.newImage( "images/staveBlankgrey2.png", 240, 160 )
        note1 = display.newImage( "images/crDown.png", 130, 141 )
        count = display.newImage( workoutTable[section][sub][data], 60, 40 ) 

            sceneGroup:insert( stave )
            sceneGroup:insert( note1 )
            sceneGroup:insert( count )

        -- goBackBtn                                   

            local widget = require ("widget")

            local button = display.newImage("images/goBackBtn.png")
            button.x = display.contentWidth / 2
            button.y = display.contentHeight - 60

            sceneGroup:insert( button )

            function button:tap(event)
                    composer.gotoScene("firstBar1", "fade", 30)
            end
            button:addEventListener("tap", button)
            print ("button 1 pressed")
            sceneGroup:insert( button )
            return true

        -- did

            elseif ( phase == "did") then
                    local function showScene2()
                        local options = {
                             effect = "slideLeft",
                             time = 30,
                                            }
            composer.gotoScene( "scene2", options )
            end

            timer.performWithDelay(tempo, showScene2 )
        end 
end     

    -- hide / destroy

    function scene:hide( event )
            local sceneGroup = self.view
            local phase = event.phase
    end

    function scene:destroy( event )
            local sceneGroup = self.view
    end

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

    return scene

If I'm missing something out could you also tell me where to include it? Thanks.


Solution

  • When you process the back button touch, you need to cancel your timer that's going to the next scene.

    At the top of the module declare a variable to store the handle to the timer:

    local sessionTimer
    

    Then when you start the timer do:

    sessionTimer = timer.performWithDelay(tempo, showScene2 )
    

    Finally if you leave the scene prior to the timer's exit, perhaps in your button handler, do:

    timer.cancel( sessionTimer )
    

    Or something similar.