views:

348

answers:

2

I have an MXML component in a website that I am reusing in a few different screens. Based on what the user clicks, the component initializes a few values and starts playing media.

Now if I click through the site, and I play media in screen 1, the component initializes fine and plays the media.

If I then go to screen 2 and play the media, the component initializes twice. Once for screen one, and once for screen 2.

When I then go to screen three, it will start initializing three times.. So it is creating a new instance of the MXML component for each screen!!

How can I make sure that the MXML component only initializes for the screen that I need it to initialize for?

What I really want is that this component always has just one instance throughout the whole application. Is it possible to make that MXML component into a Singleton, so that I always have one instance of that MXML in my application?

+2  A: 

Can you explain this a little more indepth? What do you mean by "screens"? It sounds like you have your component nested in some sort of view stack, and that your screens are different sections in the view stack, but it's hard to tell here.

Regardless, I think the solution is to abstract the part of your component that you want to be a singleton from the view. Flex initializes objects in view stacks in a lazy manner by default, but this can be overridden using the creationPolicy property on the ViewStack object. It sounds like your creationPolicy is initializing children as you access them, and something in your component code is causing other instances of the same object to re-fire some initialization code when others are created (possibly in your experimenting for an MXML singleton.)

To truly achieve your desired effect, you should probably just write a bit of actionscript that intelligently re-parents the display object you only want to be created twice. The idea of a "singleton" doesn't make as much sense when we're talking about view objects on screens - to have it displayed in many places, you need many instances, and the process of re-parenting is slightly more complicated than the singleton pattern, so you'll need to do a bit of creative logic around that.

Hope this helps - again, please feel free to post some more source code if you want a more specific response.

RJ Owen
The component is used in several places in a view stack and also on a higher level in different states of a Canvas.In my model I have a value that holds the current application state. When the user navigates, the application state gets updated.The application state value is bound to this mediaplayer, which is the MXML component I am talking about. Whenever the application state changes into an application state that needs to play media, the mediaplayer will initialize.If you have more advise based on this, please post it ;) I will start working with what you gave me so far.Thanks!
Mad Oxyn
Ok, I think I can get it working now by what you said above.Indeed some smart redesign of the component works.You were right, indeed I need more instances of the component, but by building smarter initialisation of the component I can make this work :)Thanks for your help!
Mad Oxyn
Hmm. That does sound a bit tricky. I think what you need to do is the reparenting of your video component - call removeChild() on the old parent, and then addChild() on the new one. You could certainly set this up within a singleton context so that the component itself handled it's reparenting - something like:public static getAndAddInstance(newParent:Container):void{ if(!_instance){ _instance = new thisThing(); } if(_instance.containerParent){ _instance.containerParent.removeChild(_instance); } newParent.addChild(_instance);}Something like that.
RJ Owen
sorry, can't format comments. :) I'm sure the example above there will take some hacking around to make it work, but it should get the job done. I'm still a bit confused about WHY you need to do this, but assuming you do, that should get you there. :) Good luck!
RJ Owen
+1  A: 

Why don't you make the component into a module and use it that way. You Load and/or Unload a module and use it where ever you like! in just calling it as a single item! and you have very much less overheads in your application.

aktell