views:

23

answers:

2

I have a menu, each time you click on an item it opens up a screen (a new flex component), when I click back on to the screen I want to dispose of that particular instance, is there an easy way to do this?

A: 

UIComponent.removeChild(child) is one way. You can also do this with View States.

James Ward
I think it depends how he defines dispose. removeChild won't remove all references to the component, so it won't be eligible for garbage collection. He should also consider removing an event listeners added to the component and/or nulling any variables that point to it.
www.Flextras.com
Yes. Without knowing what the component is and how it is used then it's hard to say how to get it to GC.
James Ward
A: 

The best way to do it is to store all your dynamic instances in an Array, like dynamicHandles:

var dynamicHandles:Array = new Array();
dynamicHandles["test"] = new MCTest();

And then add as children:

addChild(dynamicHandles["test"]);

Finally, whenever you need to remove them, first remove them as child and then clean up the array like so:

removeChild(dynamicHandles["test"]);
dynamicHandles = new Array();
Aurel300
I don't think that your code above will work since AS3 Arrays aren't associative. If you change dynamicHandles to an Object it would work.
James Ward