Archive for August, 2009

Flex 4 states, itemCreationPolicy and confusion

Sunday, August 9th, 2009

When you starting to work with Flex 4, you’ll soon realize that the preferred way of switching through a bunch of views is to use the newly improved <s:states> rather than the old-fashioned <mx:ViewStack>.

So what if you want to mess around with those components at startup even if the state they are assigned to isn’t active?

When I used to use viewStacks, I could set them up so that all members of the viewStack were created at startup, even if they weren’t the top on the list. All that was needed was a creationPolicy=”all” attribute on the viewStack to get everybody initializing and doing their thing.

Now, I’m trying to do the same thing when I have views associated with different states through their includeIn=”(some state)” attribute. I’d like for these views to be setup at startup even though their state isn’t active. It seems to me the Flex 4 docs suggest that this is what will happen when you use the itemCreationPolicy=”immediate” attribute/value pair.

But…sigh…it doesn’t. Here’s simple test. The first button is associated with a state that hasn’t been called yet, but since I set its itemCreationPolicy to “immediate”, you would think at least some of the event handlers would be called when the component was proactively created and cached. Not so: you’ll notice that the button’s event handlers are launched only after the state is set, regardless of the itemCreationPolicy attribute set on the button.




        
        
        
        

        

        

                
                        

        


Perhaps this isn’t a bug only my misunderstanding of some subtleties in the component lifecycle as it relates to states. But it sure seems to me that I won’t have access to my components in non-current states before that state is actually set!