back button cannot go to previous scene

I have three scenes (scene2, scene3, scene4). After I had run all three screen, i only manage to return to “screen3” from “screen4” after that i can not return to screen “screen2” from “screen3”. 

If I use “composer: gotoScene” in an event other than the back button , it works

[lua]
-----------------main.lua

local composer = require( “composer” )

composer.gotoScene(“scene2”)

-----------------scene2.lua

function scene:create( event )

    local sceneGroup = self.view

    local textScene2 = display.newText(“scene2”, 50, 50, native.systemFont, 12)

    sceneGroup:insert(textScene2)

    textScene2:addEventListener(“tap”, nextScene)

end

function nextScene(event)

    composer.gotoScene(“scene3”)

end

function scene:show( event )

    local textScene2 = display.newText(“scene2”, 50, 100, native.systemFont, 12)

    local sceneGroup = self.view

    sceneGroup:insert(textScene2)

end

-----------------scene3.lua

function scene:create( event )

    sceneGroup = self.view

    goToScene4 = display.newText(“scene3”, 50, 50, native.systemFont, 12)

    goToScene4:addEventListener(“tap”, onTapNext)

    goToScene2 = display.newText(“goScene2”, 50, 100, native.systemFont, 12)

    goToScene2:addEventListener(“tap”, onTapBack)—WORK

    sceneGroup:insert(goToScene2)

    sceneGroup:insert(goToScene4)

    Runtime:addEventListener(“key”, onKeyEvent)—NOT WORK

end

function onKeyEvent(event)—NOT WORK

    if ( event.keyName == “back” and event.phase == “up”) then

        composer.gotoScene(“scene2”)

        return true

    end

    return false

end

function onTapNext(event)

    composer.gotoScene(“scene4”)

end

function onTapBack(event)—WORK

    composer.gotoScene(“scene2”)

end

-----------------scene4.lua

function scene:create( event )

    sceneGroup = self.view

    goToScene3 = display.newText(“scene4”, 50, 50, native.systemFont, 12)

    sceneGroup:insert(goToScene3)

    Runtime:addEventListener(“key”, onKeyEvent)

end

function onKeyEvent(event)

    if ( event.keyName == “back” and event.phase == “up”) then

        composer.gotoScene(“scene3”)

        return true

    end

    return false

end

[/lua]

Your main.lua seems to be incomplete:

require "CiderDebugger";ьф

But I think your issue is that you’re setting up an onKeyEvent() multiple times and only the last one is kept.  Its really best to only define the onKeyEvent once in your main.lua and add the logic to handle tracking what scene you should be going back to.

Rob

I have a key function listener defined in main lua, this function uses a table to deal with Back button. I hope you can use that code in your project.

local function goBack (scene) local options = { effect = "zoomInOutFade", time = 400 } composer.gotoScene( scene , options) end local function onKeyEvent( event ) local keyName = event.keyName local phase = event.phase local scene = composer.getSceneName( "current" ) local backscene = { --["scripts.menu"] = function () native.requestExit() end, ["scripts.parent"] = function () goBack ("scripts.menu") end, ["scripts.select"] = function () goBack ("scripts.menu") end, ["scripts.puzzle"] = function () goBack ("scripts.select") end, ["scripts.language"] = function () goBack ("scripts.menu") end, ["scripts.aboutus"] = function () goBack ("scripts.menu") end } -- Listening for B as well so can test Android Back with B key if ("back" == keyName and phase == "down") or ("b" == keyName and phase == "down" and system.getInfo("environment") == "simulator") then --print("Back", scene) if (backscene[scene]) then backscene[scene]() return true end end if event.keyName == 's' and event.phase == 'down' and system.getInfo("environment") == "simulator" then local scene = display.captureScreen(false) --if scene then -- print( "screenshot" ) display.save(scene, {filename = display.pixelWidth .. 'x' .. display.pixelHeight .. '\_' .. math.floor(system.getTimer()) .. '.png', isFullResolution=false}) scene:removeSelf( ) return true --end end return false end Runtime:addEventListener("key", onKeyEvent)

I added the goBack() function code, that was missing in the first edit.

Your main.lua seems to be incomplete:

require "CiderDebugger";ьф

But I think your issue is that you’re setting up an onKeyEvent() multiple times and only the last one is kept.  Its really best to only define the onKeyEvent once in your main.lua and add the logic to handle tracking what scene you should be going back to.

Rob

I have a key function listener defined in main lua, this function uses a table to deal with Back button. I hope you can use that code in your project.

local function goBack (scene) local options = { effect = "zoomInOutFade", time = 400 } composer.gotoScene( scene , options) end local function onKeyEvent( event ) local keyName = event.keyName local phase = event.phase local scene = composer.getSceneName( "current" ) local backscene = { --["scripts.menu"] = function () native.requestExit() end, ["scripts.parent"] = function () goBack ("scripts.menu") end, ["scripts.select"] = function () goBack ("scripts.menu") end, ["scripts.puzzle"] = function () goBack ("scripts.select") end, ["scripts.language"] = function () goBack ("scripts.menu") end, ["scripts.aboutus"] = function () goBack ("scripts.menu") end } -- Listening for B as well so can test Android Back with B key if ("back" == keyName and phase == "down") or ("b" == keyName and phase == "down" and system.getInfo("environment") == "simulator") then --print("Back", scene) if (backscene[scene]) then backscene[scene]() return true end end if event.keyName == 's' and event.phase == 'down' and system.getInfo("environment") == "simulator" then local scene = display.captureScreen(false) --if scene then -- print( "screenshot" ) display.save(scene, {filename = display.pixelWidth .. 'x' .. display.pixelHeight .. '\_' .. math.floor(system.getTimer()) .. '.png', isFullResolution=false}) scene:removeSelf( ) return true --end end return false end Runtime:addEventListener("key", onKeyEvent)

I added the goBack() function code, that was missing in the first edit.