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?

[lua] 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 )[/lua] [import]uid: 46082 topic_id: 34750 reply_id: 334750[/import]

I think we need to see your code for where you’re transitioning scenes, clearing listeners, etc.
[import]uid: 199310 topic_id: 34750 reply_id: 138180[/import]

I have loading screens to give the player instructions like this:
[lua]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()[/lua]
I use the Android key event “back” with the code above. Then remove everything with this:
[lua] unloadMe = function()

Runtime:removeEventListener( “key”, onKeyEvent )
if theTimer then timer.cancel( theTimer ); end

if loadingImage then
loadingImage:removeSelf()
loadingImage = nil
end

end[/lua]

then I return the localGroup at the end. [import]uid: 46082 topic_id: 34750 reply_id: 138185[/import]

what calls unloadMe() maybe it would help to see all of this in context. [import]uid: 199310 topic_id: 34750 reply_id: 138190[/import]

Here it is all at once:
[lua]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[/lua]
I’ve actually disabled the Android back Button here and it still does the same thing. Am I removing it wrong? [import]uid: 46082 topic_id: 34750 reply_id: 138201[/import]

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.
[import]uid: 199310 topic_id: 34750 reply_id: 138216[/import]

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. [import]uid: 46082 topic_id: 34750 reply_id: 138219[/import]

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.

[import]uid: 199310 topic_id: 34750 reply_id: 138221[/import]

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

http://www.speedyshare.com/5akhq/backButtonTest.zip [import]uid: 46082 topic_id: 34750 reply_id: 138225[/import]

Any idea what I messed up? Im stuck [import]uid: 46082 topic_id: 34750 reply_id: 138402[/import]

I’m going to move this to the Director forum. Maybe someone with some director experience can chime in. [import]uid: 199310 topic_id: 34750 reply_id: 138415[/import]

Okay, this is my last problem before I publish to Android. Thanks for all your help! [import]uid: 46082 topic_id: 34750 reply_id: 138418[/import]

I think we need to see your code for where you’re transitioning scenes, clearing listeners, etc.
[import]uid: 199310 topic_id: 34750 reply_id: 138180[/import]

I have loading screens to give the player instructions like this:
[lua]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()[/lua]
I use the Android key event “back” with the code above. Then remove everything with this:
[lua] unloadMe = function()

Runtime:removeEventListener( “key”, onKeyEvent )
if theTimer then timer.cancel( theTimer ); end

if loadingImage then
loadingImage:removeSelf()
loadingImage = nil
end

end[/lua]

then I return the localGroup at the end. [import]uid: 46082 topic_id: 34750 reply_id: 138185[/import]

what calls unloadMe() maybe it would help to see all of this in context. [import]uid: 199310 topic_id: 34750 reply_id: 138190[/import]

Here it is all at once:
[lua]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[/lua]
I’ve actually disabled the Android back Button here and it still does the same thing. Am I removing it wrong? [import]uid: 46082 topic_id: 34750 reply_id: 138201[/import]

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.
[import]uid: 199310 topic_id: 34750 reply_id: 138216[/import]

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. [import]uid: 46082 topic_id: 34750 reply_id: 138219[/import]

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.

[import]uid: 199310 topic_id: 34750 reply_id: 138221[/import]

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

http://www.speedyshare.com/5akhq/backButtonTest.zip [import]uid: 46082 topic_id: 34750 reply_id: 138225[/import]