I am trying to create a navigation menu history button that persists throughout an app similar to the Android action bar. As the user goes through the app, the navigation menu’s history button will change its target. For example…
menu.lua -> no history / back button
scene1.lua -> history goes back to menu.lua on “tap”
scene2.lua -> history goes back to scene1.lua on “tap”
I’d like to control the history. Where I’m having a problem is switching scenes from my external module I’ve created which is called in every scene.
I set up an environment.lua file as follows (trimmed down for readability):
--environment.lua local Environment = {} local navBar local function new( backTo ) local backTo = backTo or nil navBar = display.newGroup() local btn = display.newRect( 0, 0, 44, 44 ) navBar:insert( btn ) function navBar:tap( event ) if( backTo ) then --Go to scene defined in backTo end end navBar:addEventListener( "tap", navBar ) end Environment.new = new return Environment
In my scene1.lua file, I call the above with:
--scene1.lua local environment = require( "environment" ) function scene:enterScene( event ) local view = self.view environment.new( "menu" ) end function scene:exitScene( event ) local view = self.view environment.destroy() --removes and nils the navBar package.loaded["environment"] = nil end
(I also have a destroy function and unload the package in each scene.) This had zero memory leaks and worked perfectly printing to the console on the tap…however, when I try to require the storyboard and then gotoScene in the tap function, I start leaking memory, scenes don’t purge, and I am getting double (and then some) tap events as though the navBar is loading on top of itself from scene to scene.
How can I change the scene using a button loaded from this external module?