Hi 09.SemperFidelis,
So I quickly created a dummy app, inserted your code, inserted some debugging print statements, and added it to Github. Go to the following page and download or clone the app so you can run it for yourself.
https://github.com/doubleblacktech/Corona-SDk-Storyboard-Sample
-
I placed your code in the second view - view2.lua
-
The print() statements in the code will help you understand the flow of scene execution, appearance, and removal as you go from one scene to the next.
-
I placed the following code in the enterScene handler in all three of the views:
[lua]
local previous_scene_name = storyboard.getPrevious()
if previous_scene_name ~= nil then
storyboard.removeScene( previous_scene_name )
end
[/lua]
- So if you run the app you will be presented with the first scene and a tab bar (it’s only there for demonstration purposes). As you navigate to the second screen you will see the following in your terminal (assuming you started your Corona session with the Terminal)
2013-06-14 08:13:14.354 Corona Simulator[21447:707] view2: OUTSIDE
2013-06-14 08:13:14.355 Corona Simulator[21447:707] view2: createScene()
2013-06-14 08:13:14.356 Corona Simulator[21447:707] WARNING: Map views are not supported in the simulator. Please build for device.
2013-06-14 08:13:14.378 Corona Simulator[21447:707] view2: enterScene()
Ignore the third line because we know that the Map view can not be rendered in the Corona Simulator. I had to build for XCode in order to test and make sure it was working correctly (Wack Wack Golf sounds fun!). Notice that first, any code outside the createScene is executed. Second, the code within the createScene in executed. Third, the code in enterScene is executed. By that time the first scene is now off the screen and we can remove it thus saving memory. Now if you go to the third screen you will see in the Terminal:
2013-06-14 08:26:42.225 Corona Simulator[21447:707] view2: exitScene()
2013-06-14 08:26:42.226 Corona Simulator[21447:707] view3: createScene()
2013-06-14 08:26:42.241 Corona Simulator[21447:707] view3: enterScene()
2013-06-14 08:26:42.241 Corona Simulator[21447:707] removeScene( view2 )
2013-06-14 08:26:42.241 Corona Simulator[21447:707] view2: destroyScene()
The exitScene handler in view2 is called. Then the createScene in view3 is called. Then the enterScene in view3 is called. At this point view2 is now off screen and we can nor remove it from memory. When the removeScene() method is called it triggers the destroyScene handler on view2. At this point the entire scene including the Map is removed from the view and memory.
Ok. Now go back to the second screen and you will see this in the Terminal:
2013-06-14 08:30:13.641 Corona Simulator[21447:707] view3: exitScene()
2013-06-14 08:30:13.642 Corona Simulator[21447:707] view2: OUTSIDE
2013-06-14 08:30:13.643 Corona Simulator[21447:707] view2: createScene()
2013-06-14 08:30:13.643 Corona Simulator[21447:707] WARNING: Map views are not supported in the simulator. Please build for device.
2013-06-14 08:30:13.671 Corona Simulator[21447:707] view2: enterScene()
Now this is important! The exitScene handler in view3 is called. Any code outside the createScene in view2 is executed - again. Then the code within the createScene in view2 in executed - again. This allows for the recreation of the Map and all other display elements. And finally the enterScene handler is triggered. Now here is where things get interesting. If I change the removeScene statement in view3 to purgeScene then I get the following in the Terminal when I navigate from view3 back to view 2:
2013-06-14 08:38:23.161 Corona Simulator[21447:707] view3: exitScene()
2013-06-14 08:38:23.162 Corona Simulator[21447:707] view2: createScene()
2013-06-14 08:38:23.163 Corona Simulator[21447:707] WARNING: Map views are not supported in the simulator. Please build for device.
2013-06-14 08:38:23.170 Corona Simulator[21447:707] view2: enterScene()
Whoa! None of the code OUTSIDE the createScene in view2 is being executed. If you defined any local variables outside the createScene they will still persist and be available inside the createScene handler BUT any local methods defined OUTSIDE the createScene handler WILL NOT be executed again. Something to remember. You may see some extra print statements in the code and the Terminal output that demonstrates this point.
Now what if I manually remove the Map (mapView:removeSelf()) after the transition from view2 to view3 or view1? You must recreate it either just before view2 enters or sometime after because the Map was removed due to the removeScene method.
Take a look at the code. Play around with it. I had trouble understanding Storyboard scenes and native widgets until I spent a day playing around with a simple app such as this.
Now this is what I normally do for apps that do not have a great number of display elements on each of the scenes. Removing and recreating the entire scene in larger apps or graphic intensive can introduce a noticeable delay when transitioning between scenes. This is just one in many ways to handle situations such as this.
How large or intensive will your app be? I hope this helps.