开发者

Is there an event that is triggered when a component is no longer displayed on the screen?

开发者 https://www.devze.com 2023-02-05 19:26 出处:网络
I have a custom component I\'ve developed that\'s part of a dashboard. It does some polling based on a timer that\'s part of the component.

I have a custom component I've developed that's part of a dashboard.

It does some polling based on a timer that's part of the component.

When the user navigates away from the view that contains this component I would like to stop t开发者_JAVA百科he timer and hence stop the polling.

I can obviously fire an event when the view's changed and catch it within the component but I was hoping that there might be a way to contain this all within the component.

Is there an event or state change within a component that triggers and even when a component is currently be displayed?

Thanks in advance for any help or suggestions!

Example:

        ]]>
        </mx:Script>

        <mx:TabBar x="10" y="10" dataProvider="viewstack1">
        </mx:TabBar>

        <mx:ViewStack x="0" y="0" id="viewstack1" width="675" height="315">
                <mx:Canvas label="View 1" width="100%" height="100%">
                        <mx:Button x="74" y="69" label="Button 1" width="429" height="185" removedFromStage="removeFromStageEvent()"/>
                </mx:Canvas>
                <mx:Canvas label="View 2" width="100%" height="100%">
                        <mx:Button x="74" y="69" label="Button 2" width="429" height="185" color="red"/>
                </mx:Canvas>        
        </mx:ViewStack>

</mx:Application>


removedFromStage is triggered when a component is about to be removed from the stage.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2#REMOVED_FROM_STAGE

Dispatched when a display object is about to be removed from the display list, either directly or through the removal of a sub tree in which the display object is contained. Two methods of the DisplayObjectContainer class generate this event: removeChild() and removeChildAt().


You may be able to use the focus events in combination with a hit test of an invisible sprite that covers the whole stage and/or the view/component visiblity (may need a chain of these).


EDIT: If you're trying to determine when the user switches between View 1 and View 2 in your ViewStack, you could add an event listener to the viewstack1's change event.

<fx:Script>
  <![CDATA[

    protected function viewstack1_changeHandler(event:IndexChangedEvent):void
    {
      // Do Something
    }

  ]]>
</fx:Script>

<mx:TabBar x="10" y="10" dataProvider="viewstack1" />

<mx:ViewStack x="0" y="0" id="viewstack1" width="675" height="315" change="viewstack1_changeHandler(event);">
  <mx:Canvas label="View 1" width="100%" height="100%">
    <mx:Button x="74" y="69" label="Button 1" width="429" height="185" />
  </mx:Canvas>
  <mx:Canvas label="View 2" width="100%" height="100%">
    <mx:Button x="74" y="69" label="Button 2" width="429" height="185" color="red"/>
  </mx:Canvas>        
</mx:ViewStack>

If you're using the visible property of your component to determine if it's displayed or not, you can also use the hide event handler in your component.

<local:MyComponent hide="hideHandler(event)">
  <fx:Script>
    <![CDATA[

    protected function hideHandler(event:FlexEvent):void
    {
      // Do something here.
    }

    ]]>
  </fx:Script>
</local:MyComponent>
0

精彩评论

暂无评论...
验证码 换一张
取 消