Creating deferred components

When you set a container’s creationPolicy property to none, components declared as MXML tags inside that container are not created.

For Spark containers, you use the createDeferredContent() method to manually instantiate deferred components. This method is defined on the spark.components.SkinnableContainer class.

For MX containers, you use the createComponentsFromDescriptors() method to manually instantiate deferred components. This method is defined on the MX Container base class.

Using the createDeferredContent() method

You use the createDeferredContent() method to create deferred child components in a Spark container.

In the following example with a Spark container, the children of the Panel container are not instantiated when the application is first loaded, but only after the user clicks the button:
<?xml version="1.0"?>
<!-- layoutperformance/CreationPolicyNoneSpark.mxml -->
<s:Application     
    xmlns:fx="http://ns.adobe.com/mxml/2009"    
    xmlns:mx="library://ns.adobe.com/flex/mx"     
    xmlns:s="library://ns.adobe.com/flex/spark">

    <s:layout> 
        <s:VerticalLayout/> 
    </s:layout>

    <fx:Script><![CDATA[

        private function createButtons(e:Event):void {
            myPanel.createDeferredContent();
        }

    ]]></fx:Script>

    <s:Panel id="myPanel" title="Panel with Deferred Content" creationPolicy="none">
        <s:VGroup>
            <s:Button id="b1" label="Hurley"/>
            <s:Button id="b2" label="Jack"/>
            <s:Button id="b3" label="Sawyer"/>
        </s:VGroup>
    </s:Panel>
    
    <s:Button id="myButton" click="createButtons(event)" label="Create Buttons"/>

</s:Application>

The executing SWF file for the previous example is shown below:

Only SkinnableContainer and containers that extend the SkinnableContainer class support deferred instantiation; Groups do not. When a Group is created, its children are always created. You cannot specify a creation policy on a Group.

Using the createComponentsFromDescriptors() method

Note: The createComponentsFromDescriptors() method is for MX containers. For Spark containers, use the createDeferredContent() method.

You use the createComponentsFromDescriptors() method of an MX container to create all the children of a container at one time.

The createComponentsFromDescriptors() method has the following signature:

container.createComponentsFromDescriptors(recurse:Boolean):Boolean

The recurse argument determines whether Flex should recursively instantiate children of containers that are children of the top-level container. Set the parameter to true to instantiate children of the containers, or false to not instantiate the children. The default value is true.

On a single-view MX container, calling the createComponentsFromDescriptors() method instantiates all controls in that container, regardless of the value of the creationPolicy property.

In navigator containers, if you set the creationPolicy property to all, you do not have to call the createComponentsFromDescriptors() method, because the container creates all controls in all views of the container. If you set the creationPolicy property to none or auto, calling the createComponentsFromDescriptors() method creates only the current view’s controls and their descendents.

Another common usage is to set the navigator container’s creationPolicy property to auto. You can then call navigator.getChildAt(n).createComponentsFromDescriptors() to explicitly create the children of the n-th view.