Android Back key problem

--Android Keys function BackSelectGame() director:changeScene({scene='scene.main\_menu', arg=nil}, "loader", "scrossfade"); clean\_slate() collectgarbage ("collect") end --handle the Android hardware back and menu buttons function onBackSelectGameKeyEvent( event ) local keyname = event.keyName; if (event.phase == "up" and (event.keyName=="back" or event.keyName=="menu")) then if keyname == "menu" then print("menu function not found") --goToMenuScreen() elseif keyname == "back" then BackSelectGame(); elseif keyname == "search" then print("search function not found") --performSearch(); end end return true; end -- Add the key callback if system.getInfo( "platformName" ) == "Android" then Runtime:addEventListener( "key", onBackSelectGameKeyEvent ) end --cleaner Runtime:removeEventListener("key", onBackSelectGameKeyEvent ) 

I have a problem with android back key
now I have 4 lua scene and each scene have back function
let say the scene is S1, S2, S3, S4
S4 is the gameplay, if I press back in this scene, it will show the pause option (success) 
S3 is the story scene, I made back function to S2 here and I build it
OK S3 back to S2 when I press back button, but when I’m in S4 and press back, it also back to S2 
is like the back function on the front scene become global for all scene behind it

each back function already local and it has different name
I don’t know where’s the problem

Hi @ekajulyana11,

Please see if this thread helps:

http://forums.coronalabs.com/topic/37020-android-back-button-doesnt-go-back-or-crashes/

Brent

I have to ask a question, but can you stack Runtime handlers like that?  I put mine in main.lua and program it to figure out what scene I’m in and where I should go to, so I only have the one active.

thanks for the fast reply
I deleted all my code below and change into this:
like @Brent Sorrentino referring to me

--android key local function goBack() local bomber=true display.remove(Objects);Objects=nil Runtime:removeEventListener("key", onKeyEvent) director:changeScene("scene.select\_game", "crossfade") bomber=true end local function onKeyEvent( event ) local returnValue = true if (event.phase=="up" and event.keyName=="back" and bomber==true) then timer.performWithDelay(100,goBack,1) elseif event.phase == "ended" then print ("aaaaa") bomber=false end return returnValue end Runtime:addEventListener( "key", onKeyEvent ) 

still the same

every scene back to main_menu no matter in which scene I am
the sequence of my app is like this:
main_menu–>select_game–>story_scene–>game_scene
and the back function:
main_menu<–select_game<–story_scene| (pause)game_scene

main_menu<–select_game (success)
main_menu<–select_game<–story_scene (error) (back to main_menu without went through select_game)
and if after this^ error I press back in the main_menu, it won’t exit, it just repeat itself 
main_menu doesn’t have android back function
 

Hi @ekajulyana11,

There might be an issue with how you’ve scoped “bomber”. The one you’ve created in the “goBack()” function is not the same as the one you’re checking for in the “onKeyEvent” function… it’s localized to that specific function scope. Please go back and check your scoping and if-then conditional logic carefully.

Regards,

Brent

Hi @ekajulyana11,

Please see if this thread helps:

http://forums.coronalabs.com/topic/37020-android-back-button-doesnt-go-back-or-crashes/

Brent

I have to ask a question, but can you stack Runtime handlers like that?  I put mine in main.lua and program it to figure out what scene I’m in and where I should go to, so I only have the one active.

thanks for the fast reply
I deleted all my code below and change into this:
like @Brent Sorrentino referring to me

--android key local function goBack() local bomber=true display.remove(Objects);Objects=nil Runtime:removeEventListener("key", onKeyEvent) director:changeScene("scene.select\_game", "crossfade") bomber=true end local function onKeyEvent( event ) local returnValue = true if (event.phase=="up" and event.keyName=="back" and bomber==true) then timer.performWithDelay(100,goBack,1) elseif event.phase == "ended" then print ("aaaaa") bomber=false end return returnValue end Runtime:addEventListener( "key", onKeyEvent )&nbsp;

still the same

every scene back to main_menu no matter in which scene I am
the sequence of my app is like this:
main_menu–>select_game–>story_scene–>game_scene
and the back function:
main_menu<–select_game<–story_scene| (pause)game_scene

main_menu<–select_game (success)
main_menu<–select_game<–story_scene (error) (back to main_menu without went through select_game)
and if after this^ error I press back in the main_menu, it won’t exit, it just repeat itself 
main_menu doesn’t have android back function
 

Hi @ekajulyana11,

There might be an issue with how you’ve scoped “bomber”. The one you’ve created in the “goBack()” function is not the same as the one you’re checking for in the “onKeyEvent” function… it’s localized to that specific function scope. Please go back and check your scoping and if-then conditional logic carefully.

Regards,

Brent