Popup/Alert parent in Multi-window AIR Application

When you build a multi-window AIR application you have to care about the parent Sprite/DisplayObject you set for popups opened with the PopupManager and Alert.

In a single application you probably used this:

PopUpManager.addPopUp(yourView, Application.application as DisplayObject, modal);

Problem is when you call this from a view which lives in a native window and not in the main application window that the popup opens in the wrong window which looks pretty ugly.

The solution is simple: Use parentApplication instead. parentApplication is defined in UIComponent and is

“A reference to the Application object that contains this UIComponent instance.”

So the code should look like this:

PopUpManager.addPopUp(yourView, parentApplication as DisplayObject, modal);

The same works for Alerts which can trap you even more since the parent parameter is optional:

Alert.show("your text", "your title");

If you run this code within an opened native window the Alert will be shown in the main application window. Here you have to set the parent explicitly and cast it to Sprite instead of DisplayObject:

Alert.show("your text", "your title", 4, parentApplication as Sprite);

If you open Alert windows from classes which do not extend UIComponent (like for instance a Cairngorm Command) you do not have a reference to your parentApplication which makes it more difficult. My current approach is to path a parent parameter of type Sprite within the CairngormEvent which is dispatched by a view which extends UIComponent.

16 comments to Popup/Alert parent in Multi-window AIR Application

  • Dennis van Nooij

    I’m dispatching my Alert from the Cairngorm serviceLocator which makes things even worse. This seems to work though.

    var parent:Sprite = NativeApplication.nativeApplication.activeWindow.stage.getChildAt(0) as WindowedSystemManager).document as Sprite

  • I can imagine.
    But should ask yourself if the ServiceLocator is the right place to bring up Alerts. The ServiceLocator should only contain services …

  • Dennis van Nooij

    agreed. In fact the faults are forwarded from the Webservices to a generic fault handler, to filter out general “session expired” etc. messages. Next to that where else would you catch your “wsdl not loaded” errors?

  • Sometimes I pass over a parent parameter typed as DisplayObject. This parameter is handed over to my generic fault handler so it knows the parent for the Alert window.
    However I also don’t like this approach very much. Another option would be to open a native window which acts modal. This would mean that all other windows would be deactivated and by click could bring the alert window to front …

  • Brian Brown

    I’m having many issues with this – but I think it is related to caching the custom control we are using with addPopup. It seems that the window to parent to is not being reset properly when a later call to addPopup is used. If I don’t cache the popup’d window, memory just goes up with every use (and never gets collected), so I need to cache. Any thoughts how I can ensure the correct parent window is used?

  • Brian, the garbage collection thing could also be a bug. If you think so and can produce a little test case you could file a bug at http://bugs.adobe.com/jira.
    Otherwise I would suggest asking the flexcoders list.

  • Vtrinity

    Hi,

    This is with respect to AIR windowing system. I am working on an application where components have to added/ removed from the main application window and added to new windows.(I have WindowedApplication(Main App ) with custom chrome and there are other containers within it.)
    The issue I am facing is with Comboboxes, tooltips all (popupmanger impl) show up in the main application window rather than the popped up window. I am using mx:Windows and mx:WindowedApplication.
    It would be great if you can provide with some solution.

  • Kanwaljeet Singh Kalra

    Hey Sonke,
    thanks a lot buddy..It really made my day :)

  • Feresr

    Hi there, excuse me.. but i can’t get this code working.. for some reason.. my Alert pop up keeps appearing in my main frame..

    i’m also working with mx:Windows and Mx:WindowedApplication (mainFrame)..

  • Feresr

    Hi there, excuse me.. but i can’t get this code working.. for some reason.. my Alert pop up keeps appearing in my main frame..

    i’m also working with mx:Windows and Mx:WindowedApplication (mainFrame)..

  • It should work so I assume you are using the wrong reference to retrieve the correct parentApplication.
    Are you sure you take the UIComponent of the child window?

  • Feresr

    Thanks for asking me so soon..
    Well.. I’ve got my main application. It’s a MX:WindowedApplication, and I have a button on it.

    That button calls a mx:Window this way.

    new myWindowName().open(true);

    Now, when the Alert pops-up it appears in my main application. And that’s not supposed to happen.
    I’m writing exactly the same code that you are.

    Alert.show(“your text”, “your title”, 4, parentApplication as Sprite);

    (this code is in the window where the Alert should appear. (not the main frame)).
    Thanks again for asking me, And please, excuse my English. It’s not my native language.

  • Feresr

    And one more thing.. If i write this:

    Alert.show(“your text”, “your title”, 4, this);

    or

    Alert.show(“your text”, “your title”, 4, this as Sprite);

    my application works, I mean, The pop-up appears in the right window. But I get an error.

    TypeError: Error #1034: Type Coercion failed: cannot convert mx.managers::WindowedSystemManager@9a830b1 to mx.managers.SystemManager.

  • Sorry, no idea. Maybe you should try flexcoders mailing list to get help.

  • Feres

    Ok! no problem, thanks anyway! I’ll try to figure this out! bye :)

  • Martin

    thanks!, for the last 2 days I have been struggling with alerts and pop ups. I was throwing an alert from a “second” level pop up, but the alert didn’t showed. It was there, I could close the pop up and suddenly the alert showed.

    Now I took the time to properly tell each pop up whose the parent before calling it, and things now work properly!

    thanks again mate, I always google my problems, seldom leave a reply when a solution is found but this one deserved to be replied!.