All Flex components, including Application
,
have a common, standard life cycle. These life cycles are most easily
described and understood from the view of the events they dispatch. We are
primarily concerned with the following three major events in a component
life cycle:
The events always occur in this order: preinitialize
, initialize
, and creationComplete
.
See Chapter 19 for more details on component life cycles and events.
Components are always created from descriptors. The use of
descriptors allows the Flex framework a lot of flexibility in terms of how
and when it instantiates components. Each component that can contain child
components will have a list of descriptors for the child components. As
you’ll see in Chapter 10,
you can control the creation policy for some components. In those cases,
if you were to defer instantiation of child components, such a component
would dispatch a preinitialize
event
right away, but it would not dispatch the initialize
event until the code explicitly
created the child components from the descriptors.
The initialize
event occurs when
the child components have been created. However, at this point the layout
of the child components has not yet occurred. The initialize
event means you can successfully
reference the child components, targeting them with ActionScript
commands.
The creationComplete
event occurs
at the point when we would conventionally think of a component as being
truly complete: the child components are created, and everything is laid
out.
These life cycle events occur within all components, and we’ll
return to these events throughout the book. We mention this life cycle in
this chapter because it can help you to better understand what happens
when an application first starts. That is, once an Application
instance is created, it dispatches a
preinitialize
event. However, it hasn’t
yet created any child components. It then recursively creates child
components. Here’s an example to help you better understand the process.
If an Application
contains a VBox
that contains a Label
component, the Application
instantiates its child, the VBox
. The VBox
then dispatches a preinitialize
event. The VBox
creates its child, the Label
component. The VBox
can then dispatch an initialize
event. The Application
can then do the same. Only after
everything is laid out will the Application
dispatch a creationComplete
event.