Flex 3 Preloading Explained

Posted: January 29th, 2009 | Author: Jamie | Filed under: ActionScript, Flex | Tags: , , , |

Recently during some framework development we needed to create a custom preloader that played nicely with the main Application. Implementing this functionality will be covered in a later post, but for time being this entry is intended to illustrate in detail what is going on during the load process.

Preloading in Flex 3 can either be very easy or very complicated depending on the level of control you require. Obviously vanilla applications can use the default system and need no extra work, but implementing custom preloaders that have more complex functionality (visually or behind the scenes) can take a little while the first time round.

The classes involved

Firstly, due to the slightly confusing naming conventions used in the classes involved with preloading a few definitions are useful:

  • SystemManager - A non visual class responsible for, among many many other things, creating the Preloader instance.
  • Preloader - A non visual class responsible for managing and monitoring main SWF and RSL loading, and the creation of the visual preloader.
  • IPreloaderDisplay - An interface that must be implemented by the visual class intended to display load and initialization progress.
  • DownloadProgressBar - The default implementation of IPreloaderDisplay (The standard rounded corner box with progress bar and basic messaging appearing during load and initialization of a Flex application.)

Note: Setting a custom visual preloader (IPreloaderDisplay implementation) on a Flex application can only be done through the “preloader” attribute of the main Application node.

The Process

  1. Main swf starts loading.
  2. System manager starts up, creating an instance of the Flex framework Preloader.
  3. The Preloader creates an IPreloaderDisplay instance which listens to the Preloader for progress updates.
  4. Once the Preloader has finished its load phase it moves into the initialization phase, where FlexEvent.INIT_PROGRESS events are dispatched until a FlexEvent.INIT_COMPLETE signals that the application is ready to be displayed.
  5. When the IPreloaderDisplay hears the FlexEvent.INIT_COMPLETE event it dispatches an Event.COMPLETE, unless this is heard before its minDisplayTime has elapsed, in which case a timer is set for the remainder of the duration to ensure the minDisplayTime is respected, after which the Event.COMPLETE is dispatched signifying the end of its visual display responsibilities.
  6. The Event.COMPLETE event dispatched by the IPreloaderDisplay instance is heard by the Flex Preloader which in turn dispatches a FlexEvent.PRELOADER_DONE
  7. The FlexEvent.PRELOADER_DONE is then heard by the SystemManager triggering its preloader_preloaderDoneHandler() which adds the main Flex Application to the displayList and dispatches the FlexEvent.APPLICATION_COMPLETE event.

Boiled down to 7 steps it seems fairly simple but fathoming this from scratch took a little while. The purpose of this post is only to explain how the process works, but in later posts I’ll show how a custom IPreloaderDisplay and overridden Application allow the conglomeration of main swf and multi-stage initial asset load processes into one simple progress bar.


7 Comments on “Flex 3 Preloading Explained”

  1. 1 Fabouney said at 9:25 am on March 11th, 2009:

    Great stuff !
    I am looking forward to the continued,
    Currently, I am looking how to add a preload phase, eg: “load sounds” or in fact, this step would be for me to load external audio file.
    If someone has an idea…

  2. 2 Troy said at 1:41 pm on March 11th, 2009:

    Thanks for taking the time to explain this! I’m new to Flex and would like to create a custom preloader, but I didn’t realize how complex it actually is. Looking forward to your future posts!

  3. 3 insenoreque said at 3:22 am on March 13th, 2009:

    Thank you!

  4. 4 Bryan Dunbar said at 12:13 am on March 20th, 2009:

    I’ve been fiddling with this as well trying to make a custom HTTP Service call during the preload cycle to get some data that I want to have available to the application as soon as it starts up. Is this similar to what you are trying to post about next? I’d love an example if you’ve got one.

    Thanks,
    Bryan

  5. 5 Rotin said at 12:19 pm on May 22nd, 2009:

    i found this example with source code for flex preloader :
    http://askmeflash.com/article_m.php?p=article&id=7

  6. 6 enusujuzo said at 12:04 pm on August 24th, 2009:

    enusujuzo…

    Kerepek Lagu Top Masa Kini

  7. 7 moryvol said at 1:11 pm on September 25th, 2009:

    moryvol…

    karrine steffans mr marcus movie


Leave a Reply