Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex 4: State Change Event

Is there any event in Flex 4 that I can use to detect a state change?

like image 350
ChrisInCambo Avatar asked Oct 20 '10 23:10

ChrisInCambo


2 Answers

I know this question is old but by googling for state change events I still get here so for people that want to know:

There is a StateChangeEvent.CURRENT_STATE_CHANGE event that is dispatched by the component, so your application can also listen for that.

In your listener function you can then acces the StateChangeEvent.oldState and StateChangeEvent.newState properties.

like image 96
Siebe Avatar answered Nov 03 '22 20:11

Siebe


If you are talking about view states the answer is yes, you can listen for the enterState event like this (sorry for the simplicity of the example, it's part of a project I'm working on and I removed any relevant parts of the code):

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx"
           minWidth="800" minHeight="600"
           currentState="loading">

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;

        private function onEnterLoadingState():void{
            Alert.show("Enter the loading state.", "Application");
        }

        private function onEnterLoginState():void{
            Alert.show("Enter the login state.", "Application");
        }

        private function onEnterAddState():void{
            Alert.show("Enter the addUser state.", "Application");
        }

        private function changeState(state:String):void{
            currentState = state;
        }
    ]]>
</fx:Script>

<s:states>
    <s:State name="loading" enterState="onEnterLoadingState()"/>
    <s:State name="login" enterState="onEnterLoginState()"/>
    <s:State name="addUser" enterState="onEnterAddState()"/>
</s:states>

<s:Panel id="loadView" includeIn="loading" title="Loading">
    <s:Button label="Go to login" click="changeState('login')"/>
</s:Panel>
<s:Panel id="loginView" includeIn="login" title="Login">
    <s:Button label="Go to addUser" click="changeState('addUser')"/>
</s:Panel>
<s:Panel id="addView" includeIn="addUser" title="AddUser">
    <s:Button label="Return to loading" click="changeState('loading')"/>
</s:Panel>
</s:Application>

And there is an exitState event in case you need it. I hope this helps you.

like image 45
Diego Avatar answered Nov 03 '22 19:11

Diego