Android with Director Issue
I’ve tried every combination I could think of with Android back button code to fix this. NOTHING has worked. Example: If I’m on lvl1.lua and change to lvl2.lua with Director and press the Android “back” button RIGHT when the screen switches to the new level it fires both screens backbutton events and double loads everything. I am cancelling the listener when I leave one page to go to the next. Any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | local function onKeyEvent( event ) local returnValue = true if (event.phase == "down" and (event.keyName=="back")) then director:changeScene("lvl1") return true --Must return true inside “if” statement end if (event.phase == "up" and (event.keyName=="back")) then --handles the “up” phase on the Nook “N” button return true end if (event.phase == "down" and (event.keyName=="volumeUp")) then returnValue = false -- Keep normal function end if (event.phase == "down" and (event.keyName=="volumeDown")) then returnValue = false -- Keep normal function end return returnValue end Runtime:addEventListener( "key", onKeyEvent ) |
Replies
I have loading screens to give the player instructions like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | local showLoadingScreen = function() local loadingImage = display.newText("Loading Lvl", 130 , 160, native.systemFont, 20) loadingImage:setTextColor(255) localGroup:insert(loadingImage) local goToLevel = function() director:changeScene( "lvl1" ) end theTimer = timer.performWithDelay( 4000, goToLevel, 1 ) end showLoadingScreen() |
I use the Android key event "back" with the code above. Then remove everything with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | unloadMe = function() Runtime:removeEventListener( "key", onKeyEvent ) if theTimer then timer.cancel( theTimer ); end if loadingImage then loadingImage:removeSelf() loadingImage = nil end end |
then I return the localGroup at the end.
what calls unloadMe() maybe it would help to see all of this in context.
Here it is all at once:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | game = require( "beebegames" ) local newRetinaText = game.newRetinaText function new() local localGroup = display.newGroup() local theTimer local loadingImage local showLoadingScreen = function() loadingImage = newRetinaText( 'Get Ready' ,50 ) loadingImage.x = 240; loadingImage.y = 160 local goToLevel = function() director:changeScene( "lvll1" ) end theTimer = timer.performWithDelay( 3000, goToLevel, 1 ) end local function onKeyEvent( event ) local returnValue = true if (event.phase == "down" and (event.keyName=="back")) then return true end if (event.phase == "up" and (event.keyName=="back")) then return true end if (event.phase == "down" and (event.keyName=="volumeUp")) then returnValue = false --keeps standard Android volume control end if (event.phase == "down" and (event.keyName=="volumeDown")) then returnValue = false --keeps standard Android volume control end return returnValue end Runtime:addEventListener( "key", onKeyEvent ) showLoadingScreen() unloadMe = function() Runtime:removeEventListener( "key", onKeyEvent ) if theTimer then timer.cancel( theTimer ); end if loadingImage then loadingImage:removeSelf() loadingImage = nil end end -- MUST return a display.newGroup() return localGroup end |
I've actually disabled the Android back Button here and it still does the same thing. Am I removing it wrong?
where is unloadMe being called from?
Your remove looks correct, but I'm not sure you really need to remove it. Pehraps you should just have a flag that you set when you start changing a scene then clear when you're in your new scene to keep the key handler from doing anything.
Sorry, I don't completely understand what you mean. I don't call unloadMe. I thought having it as a global like that it would always listen to cancel that listener if I switch screens. I'll create a small sample and upload it here in about 20 min.
First since you are using Director, I don't understand it nearly as well as I do storyboard. I thought director looked for a function called "clean" to do things when it was in the process of purging a scene. I've never seen the unloadMe() function used before.
I've tested this sample and it replicates my problem perfectly. If you tap the Android "back" button RIGHT when the screen transitions it will double load everything. It takes a few tries to get it right, but it will do it. It may only happen 1 in 10 times, but that might mean bad reviews. Thanks
Any idea what I messed up? Im stuck
I'm going to move this to the Director forum. Maybe someone with some director experience can chime in.
Okay, this is my last problem before I publish to Android. Thanks for all your help!

I think we need to see your code for where you're transitioning scenes, clearing listeners, etc.