Monday, April 5, 2010

Carefull with Event.ADDED_TO_STAGE and Event.REMOVED_FROM_STAGE

Use these events very carefully because the Flex Container class re-parents children. This is done during resizing. The Container class which is the super class of HBox/VBox etc tries to create scrollbars and in the process creates something called contentPane. Here is the method in the Container class that does the reparenting...


mx_internal function createContentPane():void {
....
....
....
for (var i:int = 0; i < n; i++)
{
// use super because contentPane now exists and messes up getChildAt();
var child:IUIComponent =
IUIComponent(super.getChildAt(_firstChildIndex));
newPane.addChild(DisplayObject(child));
child.parentChanged(newPane);
_numChildren--; // required
}
}


The above code results in firing the Event.REMOVED_FROM_STAGE and Event.ADDED_TO_STAGE. This can be dangerous if you are relying on these events to do something critical like destroying the view or its associated controllers etc. I have also seen implementations where a view listens to these methods and calls a dispose method to remove listeners, unwatch bindings and cleanup memory.

In short, these events become unreliable.

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home