Dead Ink Vinyl

Musings of David L Kinney

Flex 3 addChild() and initialize()

Okay, I’ve walked through all of the code in UIComponent and Container to work this out, so I’m going to share it: the sequence of operations when addChild(child) is invoked on a container and child extends UIComponent.

Since the primary use for this knowledge is managing the initialization portion of the component life cycle, I’ve highlighted the lines where your component may extend the behavior of UIComponent to perform its own operations.

The addChild() code bounces around through a lot of the Container class hierarchy, so I’ve included the class name with the method calls inside the parent’s instance so that you know where in the class hierarchy things get handled.

For simplicity, I skip showing the parameters to method calls and I don’t expand some operations (depicted by italicized text).

  1. Container.addChild()
    1. calls Container.addChildAt()
      1. removes child’s previous parent, if present
      2. calls Container.addingChild()
        1. calls UIComponent.addingChild()
          1. calls child.parentChanged()
          2. performs style setup on child
          3. calls child.stylesInitialized()
        2. invalidates size and display list of parent
      3. calls UIComponent.$addChildAt()
        1. calls Sprite.addChild()
      4. calls Container.childAdded()
        1. parent dispatches “childrenChanged”
        2. parent dispatches “childAdd”
        3. calls child.dispatchEvent()
          1. child dispatches “add”
        4. calls UIComponent.childAdded()
          1. calls child.initialize()
            1. dispatches “preinitialize”
            2. calls child.createChildren()
            3. calls child.childrenCreated()
              1. invalidates properties, size, and display list of child
            4. calls child.initializeAccessibility()
            5. calls child.initializationComplete()
              1. sets processedDescriptors = true
                1. dispatches “initialize”

There you have it: the order of operations when you invoke Container.addChild(). I hope this proves useful.

Written by dlkinney

October 5, 2008 at 12:21 am