3 Replies Latest reply on Mar 26, 2008 7:07 PM by cxf02

    Event needed that fires once when container gets focus

      Is there event in Flex 3 that will fire only when container object (or any of its children) gets initial focus? Like when gaining focus from other containers, or their children. I looked thru docs, tried 'show', 'enterState', 'focus_in' and 'update_complete' events - they either fire whenever any children get updated/gain focus, or only once when container is initialized and rendered. Any ideas?
        • 1. Event needed that fires once when container gets focus
          cxf02 Level 1
          I'm running into the same problem, here is my post in the flexcoder Yahoo group (which will never be answered before it disapears!).
          ------------------------------------------------------------------------------------------ --------
          I have a component I created with a list and a video player. I use a vstack in the application to switch between my view components like so (for shortness I have deleted all the attributes) :

          <mx:ViewStack id="appViews" >
          <vx:HomeView id="homeView" />
          <vx:WebView id="webView" />
          <vx:ProcessView id="processView" />
          <vx:ProductView id="productView" />
          <vx:ServiceView id="serviceView" />
          <vx:CompAdvantageView id="compAdvtgView" />
          <vx:CompanyView id="compView" />

          webView has the player component and on my last foray into this type of design, I had to rely on the stack to call a method that checked if the last view selected was the one that contained the video component, and if it was, it called the .stop() method. That was not a very OO type design, but it worked. I could attach a listener and do the same thing, but still not the cleanest...

          The best OO concept would be to register an event that will fire from either the canvas that contains the component, or from the component itself (which is an HBox) when the view stack moves away from the component or the component's parent and invoke the method to stop the player . This would be a correct OO way of doing things. Let the component or the composite container dictate the behavior.

          Here are the events listed below associated with the Canvas and HBox that I have tried. However, when I switch the viewstack, the video keeps on playing (I did verify that click events worked, so the registration is working fine).

          1. deactivate - A longshot, I knew it was the meant for application level events.

          2. exitState - this looked like the money event, but NOT!

          3. removedFromStage - I knew this wouldn't work.

          4. resize - I have a wipedown effect that i thought might trigger this. Oh well...

          5. hide - I didn't think so, and I was right.

          I guess the other alternative is to use the ViewStack to dispatch an event and then register that event with the component and then look at the passed event to figure out what to do next. I can live with that, but it seems like the long way around the block.

          The morning after…

          I tried dispatching an IndexChangedEvent.CHANGE event from the application and the viewStack, for the viewStack using various launching events (creationComplete, updateComplete, initialize). The pseudo code reads like this:

          private function regEvents():void {
          appViews. addEventListener( IndexChangedEvent.CHANGE, vStackChanged);

          private function vStackChanged(e:Event):void {
          var result:Boolean = appViews.dispatchEvent(e, true, false));


          What happens when I click on my ApplicationControlBar to switch between views, is this; I get an endless loop from the vStackChanged method, which it is being called until the FLASH player bombs.

          I have tried many versions of the above and they all behave the same. I went out to the bug reporting site and looked for this issue, but I can’t find it. I will report it unless someone tells me I’m making a mistake here.


          Curtis Fisher
          Edit2 to get this thing back at the top...
          • 2. Re: Event needed that fires once when container gets focus
            goldman-x Level 1
            After digging thru docs I have an idea.

            There is activate() method in FocusManager class that gets called whenever container gets focus. It should be possible to subclass FocusManager, override activate() method and do your stuff there. You would still have to instantiate your own FocusManager class and I'm not sure how well this would work considering there's already one FocusManager instance managing the container.

            Going to sleep now... if you manage to make this work post it here pls!
            • 3. Event needed that fires once when container gets focus
              cxf02 Level 1
              Forget the bug. It was my mistake.

              Use this code in your container to capture all events as a workaround...

              override public function dispatchEvent(event:Event):Boolean {
              //do something
              return super.dispatchEvent(event);

              Let's keep this at the top for awhile. What a pain. From my point of view, this thing should work through event dispatching from the object or objects and then listening from another part of the application for the event. Not my favorite way. I'm with you, if there was a getFocus and loseFocus event to use, that would be nice.

              I'll post my workaround when I get it done, but chasing this crap down takes too much time. In the meantime see what this does for you.

              Edit4 to keep this at the top...

              Make sure to vote for that bug otherwise it might not get any attention paid to it!