Flex life cycle phases – preinitialize, initialize and creationComplete
We can develop applications without knowing the life cycle phases also but if we know these things then we can do our coding in a better way. Then we will know where to place the codes and all. So here we are going to discuss about the life cycle phases of Flex applications:
Flex – preinitialize, initialize, creationComplete and applicationComplete.
preinitialize
Preinitialize is an event, it will generate at the early stage of an application and it will create actually when the application is attached to the parent container at .At that time none of the components will initialize and none of the children also create.So, preinitialize is the first phace inthe flex application life cycle.
At the time when the flex dispatches the preinitialize event none of the children or components not yet been created.
And the event type is :
mx.events.FlexEvent.PREINITIALIZE
initialize
It will occur after the preinitialize.The initialize dispatch when the intitialization of that application is completed.And after this the immediate children of the application will create.
When initilize event will occur is based on the containers creation policy.
Flex dispatches the initialize event for a container after it attaches all the container’s direct child controls and the container’s initially required children have dispatched a preinitialize event.
The initlize will occur after the preinitialize at that time the applications or containers initial properties will set.And at that time the height and width of the application is not yet calculated so the position properties won’t set during the initialization phase of the application life cycle.
The initialize event is useful for configuring a container’s children.
This event is dispatched after preinitialize phase. And flex uses initialize tio set the internal properties of the components.
creationComplete
The creation complete event will dispatch when the application,all the immediate children and their children are initialized and created .And in creatinComplete those components or objects needed at the begining and those are done with processing are drawn to the screen.
Actually the creationcomplete won’t use for setting the layout, because it will take more time to process.
The creationComplete Event is dispatched only when the construction,properthy processing , the layout and drawing is completed.
applicationComplete
Dispatched after the Application has been initialized, Actually the applicationComplete is processed by the LayoutManager, and after the processing it will get automatically attached to the display list of the corresponding application. Actually this is the last event of application creation life cycle and it make sure that the application loaded completely .
The following is an example for the different life cycle phases of a flex application . And it demonstrate when these events will occur and when we have to use theseĀ eventsĀ in an application
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="640" height="480" initialize="trackEvent(event)" preinitialize="trackEvent(event)" creationComplete="trackEvent(event)" applicationComplete="teackEvent(event)" > <mx:Script> <![CDATA[ import mx.events.FlexEvent; import flash.utils.getTimer; [Bindable] private var trackTxt:String = ""; private function trackEvent(event:FlexEvent):void { trackTxt += (event.type + " event occured at " + flash.utils.getTimer() + "ms" + "\n"); } ]]> </mx:Script> <mx:Panel x="0" y="0" width="640" height="480" layout="absolute" title="Life Cycle Events"> <mx:TextArea x="10" y="10" width="309" height="259" editable="false" id="trackReport" text="{trackTxt}"/> </mx:Panel> </mx:Application>
The output will look like this:
So,whan a flex applicationis executing first it will call the preinitialize even then initialize, creationComplete and applicationComplete respectively.
1 comment
Cool