Your loaded application, (in the main file !) must dispatch events like this :
var event:CustomEvent = new CustomEvent();
If you want to dispatch events from a subcomponent of your loaded application, do it via a mainFile reference.
var event:CustomEvent = new CustomEvent();
The application that loads the other one must listen for events like this :
loader.source = "subApplication.swf";
//The sub application is fully loaded, now I can listen for custom events
private function handlerSubApplicationComplete(e:Event):void
var subApplication:EventDispatcher = (e.target.mx_internal::contentHolder as Loader).contentLoaderInfo.sharedEvents;
subApplication.addEventListener("myCustomEvent",handlerCustomEventReceived, false, 0, true);
Thank you for your answers. My question is really if I am not in a displayObject of the loaded application, how do I get a handle to the loaderInfo? What is the best and most reliable way to do that.
I know in the loading application I can get a handle via SWFLoader.loaderInfo. But in the loaded application, (and not in a display object) how does one get a handle to the loaderInfo.
Thanks in advance.
I'm used to make a Singleton in all applications I write and to set the reference of the main in it.
So, anywhere in my applications, I can write : mySingleton.main.loaderInfo....
Here is an example of singleton class
public class MySingleton extends EventDispatcher
private static var _instance:MySingleton ;
public function MySingleton ()
if(_instance != null)
throw new Error("Singleton can only be accessed through MySingleton.getInstance()");
//Instiante the instance
_instance = this;
public static function getInstance():MySingleton
if(_instance == null) _instance = new MySingleton();
public function get main():MyApp
public function set main(value:MyApp):void
getInstance()._main = value;
In your MyApp, in the creationComplete handler, you write :
MySingleton.getInstance().main = this;
And anywhere in your application you dispatch your events like this :
In the general sense, you can't find out. In Flex, you can try using
SystemManager.getSWFRoot to get a SystemManager that loaded the class for
the non-display object. I would recommend passing in a reference to the
SystemManager to your non-display object though.
In looking through the code, what about this?
IMarshalSystemManager then lets me call the method useSWFBridge, then let's me get a handle to swfBridgeGroup, which then let's me get a handle to the parentBridge.
It seems this is reliable and is the way the internal classes get their work done.
Does anyone see a problem with this?
The parentBridge should be:
Thank you, that worked great. For what it is worth, doing a Singleton.getInstance on the IMarshalSystemManager actually throws a runtime error. It can find the class, but then doing getInstance on that class, IMarshalSystemManager doesn't have that method, which throws a runtime error. Seems like this violates the Singleton api, but I was able to use the sharedEvents on the main application to communicate.
My question now is if I send an event, and I want the receiver of the event to be able to set some data on the event for the original caller to get, that doesn't seem to be working. I am trying to set a Boolean and a String property, but the original sender of the message isn't getting the values.
In the sender, the code looks like:
var bridgeEvent:BridgeEvent = new BridgeEvent( BridgeEvent.SUB_APPLICATION_INITIALIZED_TYPE, null, new Date().milliseconds + "" );
bridgeEvent.isRequest = true;
bridge.dispatchEvent( bridgeEvent );
// after dispatching get the response data
this._idForBridgeEvents = bridgeEvent.responseData as String;
In the receiver it does:
var eventObject:Object = event;
eventObject.isHandled = true;
eventObject.responseData = this._id;I have verified that the receiver is putting in the right data, but the sender is never seeing the Boolean or String.Is there something I can do here? I thought this was supposed to be the general approach. I can always send back another event with response data, but I'd like to avoid that here.Thanks,Irv
Maybe the event is getting cloned or marshalled unexpectedly? Use the
debugger to verify you are working with the right instance.
I am literally only doing what you see in the code. This is supposed to work across the bridge right? I went looking through the source code for flex and I could only find examples of sending data, not using "return" types as I am here.
Is there anything else that can make it get cloned? This is going across different ApplicationDomains.
If it gets re-dispatched that will clone it. Set a breakpoint at the
dispatchEvent line. Examine the event. The debugger should show you that is
is object @2398231 or whatever. Set a breakpoint in the handler, see if the
event is the same object. After setting it, use "step into" until you get
back to dispatchEvent to make sure no other handler stomped those
I probably should create a new thread, but for anyone else who has to do all this stuff, this is a nice thread with lots of answers.
Our application is like Mosaic but just for applications that the company I work for produces. (So a limited set of like 10 applications) I am trying to decide if I should call unloadAndStop on the SWFLoader when I go to another app. The business folks do want the app to stop, but I am wondering if there is another way to stop the app, but if they come back "reload" it without having to reload the SWF again. I think from looking at the Loader and SWFLoader code it knows if it has loaded the SWF file before, but I am wondering about side affects at this point. Is it safer to unload the SWF? Any memory leaks or gotchas with doing that?