I’m having some troubles with Flex with regards to changing controls on different viewstack panels. At runtime, certain controls aren’t accessible and come back as null. Here’s an example structure:
viewstack1 canvasPeople datagridPeople canvasDetails tabNavigator1 canvasPersonDetail txtLastname canvasPersonOptions comboboxOccupation
I have a ‘click’ handler in datagrid1 set up to activate canvasB (viewstack1.selectedChild = canvasB) and the detail options box (tabNavigator1.selectedChild = canvasPersonOptions). The former works OK but the latter returns an error about accessing a null object, because at the moment the user clicks on an item in the People datagrid, the tabNavigator1 is null, as is any control underneath it. (its parent, though (canvasDetails), is addressable.) Currently this is a tiny application, so it’s not like it’s waiting to load the items. Is there a way to control how Flex renders controls so they’re accessible before the user sees them? I don’t know if this is specific to the viewStack control.
Yes, this is referred to as ‘creation policy’ and it’s a property on all Container classes. To do what you we’re looking for, you can use a creation policy of ‘all’; ‘auto’ is the default.
A creationPolicy of ContainerCreationPolicy.AUTO means that the container delays creating some or all descendants until they are needed, a process which is known as deferred instantiation. This policy produces the best startup time because fewer UIComponents are created initially. However, this introduces navigation delays when a user navigates to other parts of the application for the first time. Navigator containers such as Accordion, TabNavigator, and ViewStack implement the ContainerCreationPolicy.AUTO policy by creating all their children immediately, but wait to create the deeper descendants of a child until it becomes the selected child of the navigator container.
A creationPolicy of ContainerCreationPolicy.ALL means that the navigator containers immediately create deeper descendants for each child, rather than waiting until that child is selected. For single-view containers such as a VBox container, there is no difference between the ContainerCreationPolicy.AUTO and ContainerCreationPolicy.ALL policies.
More info here:
http://livedocs.adobe.com/flex/3/html/layoutperformance_04.html
Be careful with this though: Flex picked AUTO as the default for a reason. If your app grows large, the initialization time under creationPolicy=all will be large.