In Flex, what's the difference between 'creationComplete' and 'addedToStage'?
I want to count (via google analytics) the times a pop-开发者_StackOverflowup is displayed to the user. Which event should I use to deduce that the pop-up is being displayed to the user?
If a new pop up is created every time you display one, it doesn't matter which one you use as both events will be fired upon creating. If you are reusing the same object, you should use addedToStage
as creationComplete
is dispatched only once per UIComponent
.
creationComplete
is dispatched when the component, and all of its child components, and all of their children, and so on have been created, laid out, and are visible.addedToStage
is dispatched when a display object is added to the on stage display list, either directly or through the addition of a sub tree in which the display object is contained.
So if you are reusing the same object, you will get an addedToStage
every time you display it.
Someone commented about not having to use both addedToStage and creationComplete (I needed 50 rep to respond so making a new post)
Looks like there is a case where you need to use both. For instance, when you need to update something every time a view is visited and the view is displayed on a state change. The first time the view is seen, it is the creationComplete event which fires. Using addedToStage at this point runs the risk of a child component being null. Consecutive times that the view is displayed, it will not dispatch creationComplete, only addedToStage
From my own testing, it looks like using either would work. But it would be nice to know which is the more 'correct' one to use, and just what the difference is between the two.
The first addedToStage event occurs before creationComplete so if you need to access child components you'll need to use both.
精彩评论