Flex 4: State Change Event

13,506

Solution 1

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.

Solution 2

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.

Share:
13,506
ChrisInCambo
Author by

ChrisInCambo

Web developer based in Phnom Penh, Cambodia

Updated on June 07, 2022

Comments

  • ChrisInCambo
    ChrisInCambo almost 2 years

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