Friday, March 4, 2011

Switch states in Flash application example

Here's a quick problem I had to deal with last night. If you are making a programmatic Flex application and you need to switch the application state (the states you can tie various views to in your mxml code) you will have to first get a reference to the application object.

import mx.core.FlexGlobals;
import spark.components.Application;

public class StandardClass
{

private var _application:Application = mx.core.FlexGlobals.topLevelApplication as Application;

}

Note the imports above, there are many different application objects to choose from, but if you are making a standard Flex application you will want to use the Spark object. Also, you cannot apparently reference static things in FlexGlobals without first importing it. This is different behavior then you would see in Java.

So next we just need to simply pass the application object the name of the state we wish to invoke.
javascript:void(0)

application.currentState = "State2";


And that's it! If you're interested in setting transition animations you can check out the "transistions" property of the application object to set custom animations.

No comments:

Post a Comment