Flex Initialize and the Hassles of Changing State
One of the features of Adobe Flex that gives it so much power is the ability to build proper MVC-architected projects. You can build your views with MXML, usually with some basic scripting, and keep the real code and data separate. And part of that power is the ability to create different view states for your applications. A common scenario for using different view states might be: a loading state, a main menu state, and the main application UI state. When you change states, you often need some code to fire off and do its thing - whether that be (re)initializing data, making remote procedure calls, starting an animation, starting a Timer, or any of a million other things. One of the most common ways people achieve this is by adding a listener to the initialize event in their view components. This works great because initialization is called after all the behind-the-scenes work of changing states is done in Flex.
But what happens when you go back and forth between states? For instance, let’s say you build a game that returns you to the main menu state after a game-over scenario. If you try this, you’ll find a nasty surprise that can be a real headache to track down; the initialize event listeners work just fine the first time, but never again afterwards. To put this in terms of our example, the game starts just fine the first time, but when you return to the main menu and then start a new game, nothing happens. Or worse, the game is still showing the “Game Over” screen!