There're two ways of loading and unloading modules:
ModuleLoader - higher level API
ModuleManager - lower level API
1. Using ModuleLoader
1) define a ModuleLoader
2) when the button of loading the module is clicked
3) add module load ready listener
4) define the listener method
5) to unload a module
2. Using ModuleManager
1) load the module and specify the ready event listener
2) unload the module by using the same info object
No matter what method is used, we need to make sure when the loaded Module is safe for communication. Here is how to do it: by add a creationComplete listener in moduleReadyHandler.
inside moduleCreationCompleteHandler:
Someone might encounter the memory leak issue when trying to unload a module. This is a nice article to look into:
http://blogs.adobe.com/aharui/2009/08/what_we_know_about_unloading_m.html
ModuleLoader - higher level API
ModuleManager - lower level API
1. Using ModuleLoader
1) define a ModuleLoader
<mx:ModuleLoader id="moduleLoader"/>
2) when the button of loading the module is clicked
moduleLoader.url = "SimpleModule.swf";
moduleLoader.loadModule();
3) add module load ready listener
moduleLoader.addEventListener( ModuleEvent.READY, moduleReadyHandler);
4) define the listener method
private function moduleReadyHandler( event: ModuleEvent ): void {
trace("SimpleModule is loaded");
}
5) to unload a module
moduleLoader.unloadModule();
2. Using ModuleManager
1) load the module and specify the ready event listener
info = ModuleManager.getModule("SimpleModule.swf");
info.addEventListener(ModuleEvent.READY, moduleReadyHandler);
info.load();
2) unload the module by using the same info object
info.addEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler);
info.unload();
No matter what method is used, we need to make sure when the loaded Module is safe for communication. Here is how to do it: by add a creationComplete listener in moduleReadyHandler.
moduleLoader.child.addEventListener(FlexEvent.CREATION_COMPLETE,
moduleCreationCompleteHandler);
inside moduleCreationCompleteHandler:
protected function moduleCreationCompleteHandler(event:FlexEvent):void
{
moduleLoader.child.removeEventListener(FlexEvent.CREATION_COMPLETE,
moduleCreationCompleteHandler);
// further processings...
}
Someone might encounter the memory leak issue when trying to unload a module. This is a nice article to look into:
http://blogs.adobe.com/aharui/2009/08/what_we_know_about_unloading_m.html
本文介绍了两种在Flash中加载和卸载模块的方法:使用ModuleLoader API和ModuleManager API。这两种方式都包括了加载模块、指定加载完成后的事件监听器、实际加载模块以及卸载模块等步骤。此外,还提到了为了确保加载后的模块能够安全地进行通信,需要添加creationComplete事件监听器。
584

被折叠的 条评论
为什么被折叠?



