Bringing focus back to a modal dialog after dismissing an alert
Avg. Rating
3.0
Tags:
Problem
If an Alert is triggered from a modal dialog, when it is dismissed, the focus of the Application goes to the parent Application, if you have disabled the modal dialog. Let's say you want to re-enable the dialog and return the focus to this modal dialog. The parent Application should be inaccessible at this point.
Solution
You need to make sure that the modal dialog is re-enabled before the Alert dismisses itself. Otherwise, once the Alert gets dismissed, the Application will look at the dialog, see that it is not enabled and will return focus to the parent Application.
Detailed explanation
In the first example (testBug.mxml), the Application does not set its focus correctly when an Alert is dismissed. The Application does the following:
Click on the PopUp button to launch a modal dialog.
On creationComplete, this dialog triggers an Alert. The modal dialog is disabled.
On the close event for the Alert, the dialog is enabled and a call to setFocus is made for the dialog.
Problem: When the setFocus call is made, the dialog was still disabled, so the focus is set on the main Application. If you tab after dismissing the Alert, your focus will be in the main parent Application, which is undesireable.
In the second example (solution.mxml), the Application does set the focus back to the modal dialog. The Application does the following:
Click the PopUp button to launch a modal dialog.
On creationComplete, this dialog triggers an Alert. The modal dialog is disabled. A click handler is added to the Alert.
On the click handler of the Alert, the modal dialog is re-enabled.
On the close event for the Alert, the dialog a call to setFocus is made for the dialog.
In this example, the focus is back on the modal dialog rather than the parent Application.
Download the sample files listed below to see the code for this solution.
modalDialogFocus.zip [One Application to show the problem (testBug.mxml) and one with the solution (solution.mxml)]