Search code examples
apache-flexactionscript-3browserflex4

How to refresh an application in Flex?


I have designed a quiz application in Flex 4. At the end I want to reload my application (that is refresh the page in the browser). At the end I will show score in an alert. After that I want to reload the current application. How can I do this?


Solution

  • To cause the refresh to not happen until after your alert is clicked:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   xmlns:s="library://ns.adobe.com/flex/spark" xmlns:local="*" 
                   >
        <fx:Script>
            <![CDATA[
                import mx.controls.Alert;
                import mx.core.FlexGlobals;
                import mx.events.CloseEvent;
                protected function refreshClicked(event:Event):void
                {
                    Alert.show("Congratulations you won", 
                        "Hooray!", 
                        Alert.NO|Alert.YES, null, refreshFinish);
                }
                protected function refreshFinish(event:CloseEvent=null):void{
                    if(event == null){
                        event = new CloseEvent("refreshFinish");
                        event.detail = Alert.YES;
                    }
                    if(event.detail == Alert.YES){
                        navigateToURL(new URLRequest(FlexGlobals.topLevelApplication.url), "_self");
                    }
                }
            ]]>
        </fx:Script>
        <s:Button label="Alert and Refresh" click="refreshClicked(event)" />
    </s:Application>
    

    You can remove the option of "NO" by removing it from the or as the 3rd parameter of Alert.show.