3 Replies Latest reply on Jun 8, 2011 6:38 AM by Alberto_Mz_Dz

    Unload swf file using SWFLoader

    Alberto_Mz_Dz

      Hi,

       

      I have a main application where I am using a SWFLoader to load a second application.

      In the second application I want to put a button (Close) and when I click it I want to unload this application.

      Is it possible to achieve this?

       

      I know that I can unload a swf file using the unloadAndStop method of SWFLoader, but how can I access it from the loaded application?

       

      I think that another approach would be adding eventlisteners to loaded swf but I am not sure how to do that.

       

      Does anyone can help me?

       

      Thanks in advance,

       

      Alberto

        • 1. Re: Unload swf file using SWFLoader
          Ankur Arora Level 1

          Use SWFloaderInstance.content property to get to loaded SWF. You should have a public method in loaded SWF which will unload internal swf. Call that method by MovieClip(SWFloaderInstance.content).unloadInternalSWFs();

           

          Let me know if you need any other help.

           

          Thanks

          Ankur Arora

          Flex & Flash expert

          1 person found this helpful
          • 2. Re: Unload swf file using SWFLoader
            Peter Blazejewicz Level 4

            Hi,

             

            @Alberto

             

            I think that another approach would be adding eventlisteners to loaded swf but I am not sure how to do that.

             

            I think you could use custom or built-in event type to dispatch information to hosting application and SWFLoader *has* built-in dedicated routine to unload loaded context - which includes removing event listeners added to swf-types on content load (Yes, there are means in Flex SDK to access top-most application but usually that is not advised to access methods/properties of parent as it closely ties content together).

            So your parent application could start to listen for custom event when load completes and removes loaded app that way:

             

             

            <?xml version="1.0" encoding="utf-8"?>

            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                              xmlns:s="library://ns.adobe.com/flex/spark"

                              xmlns:mx="library://ns.adobe.com/flex/mx">

                

                 <fx:Script>

                      <![CDATA[

                           protected function chidLoadedHandler(event:Event):void

                           {

                                swfLoader.content.addEventListener(Event.CLOSE, unloadChildHandler);

                           };

                           protected function unloadChildHandler(event:Event):void

                           {

                                swfLoader.content.removeEventListener(Event.CLOSE, unloadChildHandler);

                                swfLoader.unloadAndStop(true);

                           }

                      ]]>

                 </fx:Script>

                

                 <s:SWFLoader

                      width="100%" height="100%"

                      source="ChildApplication.swf"

                      complete="chidLoadedHandler(event)"

                      id="swfLoader"/>

                

            </s:Application>

             

             

            while your child application simply:

             

             

            <?xml version="1.0" encoding="utf-8"?>

            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                              xmlns:s="library://ns.adobe.com/flex/spark"

                              xmlns:mx="library://ns.adobe.com/flex/mx">

                

                 <fx:Script>

                      <![CDATA[

                           protected function unloadMeHandler(event:MouseEvent):void

                           {

                                // we will dispatch "CLOSE" event - this could be custom event type

                                // but I think it is just enoguht heres

                                var closeEvent:Event = new Event(Event.CLOSE);

                                // root is top-most - so it is "content" top-most display object as well

                                this.root.dispatchEvent(closeEvent);

                           }

                      ]]>

                 </fx:Script>

                 <!-- ui -->

                 <s:Button x="10" y="10" label="Unload me" click="unloadMeHandler(event)" id="unloadButton"/>

            </s:Application>

             

             

             

            Not sure if that is most bullet proof or recommended solution but could be place to start,

            regards,

            Peter

            • 3. Re: Unload swf file using SWFLoader
              Alberto_Mz_Dz Level 1

              Hi Peter,

               

              Thanks for the info, it was very helpful.

               

              That's what I needed.

               

              Regards,

               

              Alberto