I hope I'm not being presumptuous, but I think what Michael might be getting at is "is initialize() is the lifecycle method in which I add my event listeners, where's the corresponding lifecycle method in which I remove those event listeners?" I'm not familiar with Flex's component lifecycle, but if this is indeed what you're thinking Michael, then I'd offer that initialize only means it's been processed by the serializer and that after that point, it's up to the app to know when the component can safely be "un-wired".
-T On Wed, Dec 30, 2009 at 9:30 PM, Greg Brown <[email protected]> wrote: > Listeners can be removed by calling the remove() method on the listener > list, similar to Swing and WPF. > > On Dec 30, 2009, at 9:26 PM, Michael Bushe wrote: > > > Greg, > > > > How/when can event listeners be removed (to avoid "memory > leaks"/"loitering > > objects")? Flex has a robust component lifecycle, I don't remember Pivot > > having a component lifecycle model at all (and neither does Swing, except > > for Window, you have to bolt it on yourself). I think I'd rather see the > > whole model at once, I suppose in 1.5. > > > > I'm a HUGE fan of what you are trying to do, but maybe it should wait? > > OTOH, you know you are going to have initialize(), maybe that's a good > > start. On the third hand, if you add preInitialize() later (like Flex), > > then existing initialize() calls aren't broken exactly, but when things > > happen may shift and be a surprise. > > > > Michael Bushe > > Software Architect/Developer > > [email protected] > > www.bushe.com > > > > > > On Wed, Dec 30, 2009 at 9:14 PM, Greg Brown <[email protected]> wrote: > > > >> FYI, though it wasn't mentioned in the commit message, this change also > >> included a minor addition/change to the org.apache.pivot.wtkx package. > This > >> change is relatively insubstantial but should actually have a > significant > >> impact on "real" application development. > >> > >> Some background: In Flex, MXML files are compiled to classes. So, this > file > >> generates a class named MyVBox: > >> > >> MyVBox.mxml: > >> > >> <mx:VBox initialize="onInitialize()" > >> xmlns:mx="http://www.adobe.com/2006/mxml"> > >> <mx:Script> > >> public function onInitialize():void { > >> // Wire up event handlers, etc. > >> } > >> > >> public function onMyButtonClick():void { > >> // Respond to button click > >> } > >> </mx:Script> > >> > >> <mx:Button id="myButton" label="Click Me" > click="onMyButtonClick()"/> > >> </mx:VBox> > >> > >> I can then use that class as a component in other MXML files (for > example, > >> I could add it to a tab pane, or "TabNavigator" in Flex). > >> > >> In Pivot, something similar could be done with the following WTKX: > >> > >> my_box_pane.wtkx: > >> > >> <BoxPane orientation="vertical" > >> xmlns:wtkx="http://pivot.apache.org/wtkx" > >> xmlns="org.apache.pivot.wtk"> > >> <wtkx:script> > >> function myButtonClicked() { > >> // Respond to button click > >> } > >> </wtkx:script> > >> > >> <PushButton wtkx:id="myButton" buttonData="Click Me" > >> ButtonPressListener.buttonPressed="myButtonClicked()"/> > >> </BoxPane> > >> > >> We can then include my_box_pane.wtkx into a TabPane declared in another > >> WTKX file. However, Pivot didn't have an equivalent of onInitialize(). > If > >> the root element is a Window, we could listen for windowOpened(), but > >> otherwise we're stuck. > >> > >> Also, it becomes more complex when we want to associate Java code with > WTKX > >> includes, rather than putting the logic in script. This is something any > >> non-trivial application is going to want to do, and Pivot didn't provide > an > >> easy way to do it. The change I checked in attempts to solve these two > >> problems. We can't use WTKX to define new classes, but we can use WTKX > to > >> instantiate classes we have defined elsewhere. For example: > >> > >> MyBoxPane.java: > >> > >> public class MyBoxPane extends BoxPane implements Bindable { > >> @WTKX private PushButton myButton = null; > >> > >> public MyBoxPane() { > >> super(Orientation.VERTICAL); > >> } > >> > >> @Override > >> public void initialize() { > >> myButton.getButtonPressListeners().add(new > ButtonPressListener() > >> { > >> public void buttonPressed(Button button) { > >> // Respond to button click > >> } > >> }); > >> } > >> } > >> > >> Using this approach, the updated my_box_pane.wtkx would look like this: > >> > >> my_box_pane.wtkx: > >> > >> <foo:MyBoxPane orientation="vertical" > >> xmlns:wtkx="http://pivot.apache.org/wtkx" > >> xmlns:foo="com.foo" > >> xmlns="org.apache.pivot.wtk"> > >> <PushButton wtkx:id="myButton" buttonData="Click Me"/> > >> </foo:MyBoxPane> > >> > >> Once WTKXSerializer is done processing foo:MyBoxPane, it sees that it > >> implements Bindable and bind()s to it. It then calls initialize() to > allow > >> the class to register event listeners and perform any other required > >> initialization tasks. > >> > >> This is a late-breaking change for 1.4, so please let me know ASAP if > you > >> have any questions or concerns. > >> > >> G > >> > >> > >
