I ran into this same problem. It's a shame there isn't a formal way for
dealing with this in the framework. Here's my solution...

Step 1: Extend the State component. The main difference being an event
handler listening for FlexEvent.EXIT_STATE to remove children
package blah.blah {
     import mx.states.State;
     import mx.states.IOverride;
     import mx.events.FlexEvent;
     import mx.core.mx_internal;
     import mx.core.UIComponent;
     import mx.states.AddChild;

     public class StateDestroy extends State {
         public function StateDestroy() {
             super();
             this.addEventListener(FlexEvent.EXIT_STATE, destroyChildren,
false, 99);
         }

         private function destroyChildren(event:FlexEvent):void {
             for each (var override:IOverride in this.overrides) {
                 if (override is AddChild) {
                    
AddChild(override).remove(UIComponent(AddChild(override).target.parent))\
;
                     AddChild(override).target = null;
                     AddChild(override).mx_internal::instanceCreated =
false;
                 }
             }
         }
     }
}


Step 2: Create a class which implements the IDeferredInstance interface.
The catch here is to not reuse the existing instance when asked for
'getInstance' but to return a new instance...
package blah.blah {
     import mx.core.IDeferredInstance;

     public class DeferredInstanceCustom implements IDeferredInstance {
         private var generator:Class;

         public function DeferredInstanceCustom(generator:Class) {
             this.generator = generator;
         }

         public function getInstance():Object {
             return new generator();
         }
     }
}

Step 3: When using the Custom State, specify AddChild elements with the
property of targetFactory
<mx:Box xmlns:mx="http://www.adobe.com/2006/mxml";
xmlns:states="blah.blah.*">
     <mx:Script><![CDATA[
         import blah.blah.DeferredInstanceCustom;
     ]]></mx:Script>
     <mx:states>
         <states:StateDestroy name="blah">
             <mx:AddChild targetFactory="{new
DeferredInstanceCustom(blah.blah.myCustomComponent)}"/>
         </states:StateDestroy>
     </mx:states>
</mx:Box>

Not the cleanest thing in the world. If anyone has other suggestions on
an cleaner implementation, please follow up! :)

Mike

--- In [email protected], Daniel Wabyick <[EMAIL PROTECTED]> wrote:
>
>
> Is there some way to have objects that were added using <mx:AddChild
/>
> completely destroyed instead of just removed from the DisplayList?
>
>
> The reason for this is that my added panels  are bound into a data
model
> and may end up performing computationally expensive tasks when they
> aren't visible. Ideally I would like to completely destroy the
component.
>
> Is this possible with the current API, or does it require some fancy
> footwork?
>
> Thanks,
> -Daniel
>

Reply via email to