SystemManager and Application Initialization

I’ve been doing a small research as to what’s happening under the hood when Flash Player 9 loads and starts a Flex 2 application. Since Flex 2 is still pretty young, there is not too many resources are available, so I’d appreciate any feedback.
The main manager that controls the application window, creates and parents the Application instance, popups, cursors, manages the classes in the ApplicationDomain, and more. The SystemManager is the first class that is instantiated by Flash Player for your application. It stores the size and position of the main application window, keeps track of its children, such as floating popups and modal windows. Using the SystemManager you can access embedded fonts, styles and the document object. SystemManager also controls application domains, which are used to partition classes by security domains (see description of the ApplicationDomain class in Flex Language Reference).
If you’ll be developing custom visual components (descendents of the UIComponent class), keep in mind that initially such components are not connected to any display list and the SystemManager=null. Only after the first call of the addChild() a SystemManager will be assigned to them. You should not access SystemManager from the constructor of you component, because it can still be null.

In general, when the Application object is created, it goes through the following steps:

1. Instantiation of the Application object begins.
2. Initializes the Application.systemManager property
3. The Application dispatches the preinitialize eventat the beginning of the initialization process.
4. The method createChildren() is called on the applicatoin. At this point each of the application’s components is being constructed, and each component’s createChildren() will be also called.
5. The Application dispatches the initialize event, which indicates that all application’s components have been initialized.
6. The creationComplete event is being dispatched
7. The Application object is added to the display list.
8. The applicationComplete event is being dispatched.

For performance reasons, it is not recommended creating components in a constructor – use createChildren() for this.

As opposed to Flash movies that consist of multiple frames being displayed over a timeline, Flex SWF files have only two frames. The SystemManager, Preloader, DownloadProgressBar and a handful of other helper classes live in the first frame. The rest of the Flex framework, your application code and embedded assets like fonts and images resides in the second frame. When Flash Player initially starts downloading your SWF, as soon as enough bytes come for the first frame, it instantiates a SystemManager, which tells player to stop at the end of the frame. Then SystemManager creates a Preloader, which creates a DownloadProgressBar and these two objects are watching the rest of the byte streaming-in process. What all bytes for the first frame are in, SystemManager sends the enterFrame for the second frame, and then renders other events. Eventually, the Application object dispatches the applicationComplete event.

You may say, why do I need to know what’s happening before my application starts? This knowledge may become quite handy. For example, you can create a fading splash screen with the image of your choice by substituting the standard Flex Preloader and the DownloadProgressBar objects with the custom ones. Ted Patrick from Adobe has provided a sample code that does exactly this: displays a splash screen using an image from a from a .gif file (see http://www.onflex.org/ted/2006/07/flex-2-custom-preloaders.php).

The application tag of this sample looks pretty straightforward:

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
preloader=”preload.CustomPreloader” >

Download the code from this application and you’ll see that the classes CustomPreloader that extends the DownloadProgressBar and a helper WelcomeScreen that loads the image and fades it away are also pretty small.

The most important feature of the Flex 2 is that it’s an open and extendable framework. I’m used to be in complete control with Java, and Flex does not tie my hands either.
That’s all…for now

4 thoughts on “SystemManager and Application Initialization

  1. Very nice exploration. Let me share some of my own (still quite unanswered) question here:

    Today I’ve looked into mx.core.Application class and found there interesting metadata called [Frame] and presented with the following code (I’ve included comment too for the sake of completeness):

    //————————————–
    // Other metadata
    //————————————–

    /**
    * The frameworks must be initialized by SystemManager.
    * This factoryClass will be automatically subclassed by any
    * MXML applications that don’t explicitly specify a different
    * factoryClass.
    */
    [Frame(factoryClass=”mx.managers.SystemManager”)]

    the second class where this metadata can be found is mx.core.SimpleApplication:

    [Frame(factoryClass=”mx.core.FlexApplicationBootstrap”)]

    I couldn’t find any explanation about Flex 2 [Frame] metatag nor in the documentation, neither in the web search results.

    My opinion is that [Frame] metatag is used to add a frame into Flex application. In this case, Application and SimpleApplication classes use [Frame] metadata to add corresponding system managers into first frame as the starters of application initialization process. And this fits into your above explanation.

    And probably this can be used to add new frames into Flex 2 application.

    But my knowledge of Flex 2 Framework is not quite deep to make sure I’m right, so it is interesting what do you think about this [Frame] Flex 2 metadata?

  2. Rost,

    Better link to the Roger’s Max demo is here – he refers to frame tag within the few last pages: http://blogs.adobe.com/rgonzalez/.

    Basicaly it is just a reguest to call bootstrap manager which in turn will execute constructor. That in turn allows you to write your own initialization code/register with the application asynchronously. I believe the book excerpt explaining this in details was published by one of sys-con magazines – I can find only part one out of 2 or 3 here http://webddj.sys-con.com/read/295397.htm. Article tells you about underwater issues you have to be concerned about, but the process is quite straight forward.

    Hope this helps
    Anatole

  3. Yakov, as i understood, Matt Chotin is the second person at Adobe who shares this secret modular knowledge :-)
    Thank you guys for brilliant links which bring such a valuable info to me!
    I will go further in digging this hole.

Comments are closed.