Back button problem

Hello everybody,

I try to find a solution since two days, but i don’t know how i can. 
My problem is that in my app, i have : 

the view 1 (home menu) and a other view the view 2. 

When i’m in the view 2, i put on my code the function which use the back button to come back to the menu. 

This function work but, when the app is on the first view (home menu) the function back button continue to work and results the function back button of my view 1. 

As if i press the back button TWICE ! I hope that you understand my problem. 

back button of my view1 (home) : 

local function onComplete( event ) if "clicked" == event.action then local i = event.index if 1 == i then os.exit() --- execute la fonction quit qui quitte le jeu elseif 2 == i then storyboard.gotoScene( "view1" ) -- execute la fonction reset c'est à dire rej end end end local function onKeyEvent( event ) local keyName = event.keyName local phase = event.phase if (keyName == "back" and phase == "down") then local alert = native.showAlert( "Bordeaux Privilèges", "Vous souhaitez quitter l'application", { "Quitter", "Annuler" }, onComplete ) return true end return false end Runtime:addEventListener("key", onKeyEvent)

back button on my view 2, to come back to the menu : 

local function onKeyEvent( event ) local keyName = event.keyName local phase = event.phase if (keyName == "back" and phase == "up") then storyboard.gotoScene("view1"); return true end return false end Runtime:addEventListener("key", onKeyEvent)

It’s best to only have one onKeyEvent listener in your main.lua.  You can get the current scene name and build in logic to handle different behaviors based on the scene you are in.

Rob

i see what you mean, but i don’t find the good way to code this, always the same problem

local function onKeyEvent( event )     local keyName = event.keyName     local phase = event.phase       if (keyName == "back" and phase == "down") then         if composer.getSceneName( "current" ) == "view1"  then             local alert = native.showAlert( "Bordeaux Privilèges", "Vous souhaitez quitter l'application", { "Quitter", "Annuler" }, onComplete )          else              storyboard.gotoScene("view1")          end          return true      end     return false end Runtime:addEventListener("key", onKeyEvent)

See how you check to see if you are in you main menu scene (view1) and then show your alert, if you are not, then you just go to your view1 scene.   This can go into your main.lua and then you don’t have to deal with it elsewhere in your code.    The problem when you try and do it in the scene is if you can’t get it removed, you end up with too many of them running.  And the order that the scenes change doesn’t give you a good way to clear them.

Rob

It’s best to only have one onKeyEvent listener in your main.lua.  You can get the current scene name and build in logic to handle different behaviors based on the scene you are in.

Rob

i see what you mean, but i don’t find the good way to code this, always the same problem

local function onKeyEvent( event )     local keyName = event.keyName     local phase = event.phase       if (keyName == "back" and phase == "down") then         if composer.getSceneName( "current" ) == "view1"  then             local alert = native.showAlert( "Bordeaux Privilèges", "Vous souhaitez quitter l'application", { "Quitter", "Annuler" }, onComplete )          else              storyboard.gotoScene("view1")          end          return true      end     return false end Runtime:addEventListener("key", onKeyEvent)

See how you check to see if you are in you main menu scene (view1) and then show your alert, if you are not, then you just go to your view1 scene.   This can go into your main.lua and then you don’t have to deal with it elsewhere in your code.    The problem when you try and do it in the scene is if you can’t get it removed, you end up with too many of them running.  And the order that the scenes change doesn’t give you a good way to clear them.

Rob