请大家定位到Flex3InstallRoot\sdks\3.0.0\frameworks\projects\framework\src\mx\managers\PopUpManagerImpl.as中,看看人家究竟是怎么个设计。
大家请先看
public function addPopUp 这个函数
里面一对代码都是关于如何在PopupManager中创建一个弹出窗口的代码,其中有一点很值得重视…
// Listen for unload so we know to kill the window (and the modalWindow if modal)
// this handles _all_ cleanup
window.addEventListener(Event.REMOVED, popupRemovedHandler);
就是在这里,PopupManager给添加了个Remove事件 的监听器。
跟着,如果我们要去除这个PopUp的时候是调用RemovePopup的,我们看看这个函数是怎么写的
public function removePopUp(popUp:IFlexDisplayObject):void
{
// all we want to do here is verify that this popup is one of ours
// and remove it from the display list; the REMOVED handler will do the rest
// (this is so that we never leak memory, popups will self-manage even if
// removePopUp is not called).
if (popUp && popUp.parent)
{
const o:PopUpData = findPopupInfoByOwner(popUp);
if (o)
{
const sm:ISystemManager = ISystemManager(popUp.parent);
if (o.topMost)
sm.popUpChildren.removeChild(DisplayObject(popUp));
else
sm.removeChild(DisplayObject(popUp));
}
}
}
看见开发 人员自己写的注释没?他意思是说,当执行removeChild的时候,remove时间会自动触发,来给他们做剩下的清理工作。
跟着我们看看popupRemovedHandler
毫无疑问,它对里面的对象 做了一些清理工作,具体就看代码吧......
这个类告诉我们一个很明显的道理:
任何一个DisplayObject被移除的时候都会发出Remove事件。我们只要对这个remove事件进行监听,再进行相关的清理工作就可以了。
养成良好的编程习惯。注意一写变量定义范围。不用的对象,事件要及时删除。