I'm not sure if this method is way off base or not, but it worked. In this case, I wanted to specify a different class to be returned when calling ResourceManager.getInstance(). Currently, flex apps are pretty much hardcoded to return an instance of the mx.resources.ResourceManagerImpl class.

How does it that method know to return an instance of that class? Well, the Flex framework uses a class called Singleton, which keeps a registry that maps interface names to classes that are supposed to be singletons in the app. So, ResourceManager.getInstance() calls Singleton.getInstance() to ask if for the IResourceManager class to use in the app:

Actionscript:
  1. instance = IResourceManager(Singleton.getInstance("mx.resources::IResourceManager"));

The problem I ran into is that for IResourceManager, and a few other interfaces, the class to return is registered in the SystemManager.initialize() method, which runs way before any of the actual flex application code is available.

However, there is one custom class which can be accessed during the SystemManager.intialize() method: the custom preloader class. The preloader class is accessed a few lines above where Singleton.registerClass() is called to register the IResourceManager singleton.

So, I made a subclass of DownloadProgressBar and set that to be the preloader class. That class uses a static initializer to register my own class to use for ResourceManager.getInstance():

Actionscript:
  1. private static var classConstructed:Boolean = classConstruct();
  2.    
  3.         private static function classConstruct():Boolean {
  4.             var rm:ResourceManager2;
  5.             Singleton.registerClass("mx.resources::IResourceManager",
  6.             Class(getDefinitionByName("com.venarc.designer.managers.ResourceManager2")));
  7.             return true;
  8.         }

Now my custom ResourceManager2 class is used whenever you call ResourceManager.getInstance(). I wish there were some compiler options for specifying which classes to use for those Singletons, would've made everything a lot easier.