Alert.show() behavour in flex
August 4, 2006 16 Comments
The other day, my colleague (Ajit Parthan) was telling me about a wierd behavoiur he had noticed with the Alert.show() in flex. In flex, when you throw an alert, the code execution doesnt halt.
I have myself noticed many times that when you give out multiple alerts, each alert popups one over the other. Each alert doesnt wait for the user to manually dispose it before proceeding further with the code. Is that wierd? Somehow, it never ‘clicked’ to me that this could be problem🙂. But, now I relealized that it could be a slight inconvenience. That is, it requires the developer to do things a bit differently than he is used to in other languages.
It isn’t a bug; it’s due to the way that the Flash Player works. The Player isn’t threaded and doesn’t have public APIs for processing its event queue. Alert.show() must return as soon as the Alert is shown — not when the user dismisses it — so that the Player can handle the user’s mouse and keyboard events while the Alert is up.
This is similar to what happens when you use APIs that load information over the network… they return immediately, and you get notified via an event when the data arrives.
– Gordon Smith (in Flexcoders mailing list)
To handle this, you have to write a handler function to be executed if the user clicks OK, and pass it as an arguement to the Alert.show() method. For example, if you are trying to save some user info to the backend, and you would like the user to confirm the action.
alert("Are you sure?", "Confirmation", mx.controls.Alert.OK | mx.controls.Alert.CANCEL, setUserInfo);