Alain Hufkens {Rich Interactive Applications Developer}

10Mar/0911

How to restart an AIR application from code

I am currently working on an AIR application developed using the Flex 3 SDK. The problem I am facing now, is that I need to restart my application from code. This sounds very easy, you would think. My first guess would be that there should be a method on the NativeApplication object like:

NativeApplication.nativeApplication.restart();

But this method does not exist. There is an exit method, but no restart method. So I "googled" around and came to some interesting articles describing how to do this.

The Joy of Flex blog wrote an article "Can an AIR application restart itself?" I tried it out, but this did not work for my project. The code actually steps through the methods but the application never restarts. The article also says that there are serious problems with this approach. So probably I am in the case where the exit beats the launch.

So my next approach was this article by David Tucker: "AIR Tip 11 - Launching an AIR Application from an AIR Application". It actually described how to launch an AIR application from another application. This is what has been used in the first article, so back to start.

Then I found this interesting peace of code on Flex Internals:

package
{
  import mx.core.Application;
  import mx.core.WindowedApplication;
  import adobe.utils.ProductManager;

  public function reboot():void
  {
    var app:WindowedApplication =
        WindowedApplication(Application.application);
   
    var mgr:ProductManager =
        new ProductManager("airappinstaller");
   
    mgr.launch("-launch " +
        app.nativeApplication.applicationID + " " +
        app.nativeApplication.publisherID);
   
    app.close();
  }
}

Also make sure that the "allowBrowserInvocation" option is turned on in the AIR application descriptor template:

<allowBrowserInvocation>true</allowBrowserInvocation>

So this was actually what I needed, and it worked!
Any feedback is appreciated :)

Filed under: air, flex Leave a comment
Comments (11) Trackbacks (0)
  1. Fantastic!

    Thank you so much.

    Adam McCrossan

  2. Is, “var app:WindowedApplication = WindowedApplication(Application.application)” supposed to be, ” var app:WindowedApplication = WindowedApplication(NativeApplication.nativeApplication)”?

  3. Hey Lee,

    The code provided in the blog post works for me. But it is used in a Flex application. You can see that the Application class is located in the mx.core namespace. If you have a non-Flex AIR application this namespace is not available and you will need the NativeApplication class which is located in the flash.desktop namespace.

    I haven’t tried it, but I suppose it should also work.

  4. Unfortunately I don’t think it will work with a non-Flex app, without the WindowedApplication class. Attempts at using the WindowedApplication class from Flash are resulting in a number of import errors, even with the Flex SDK swc files included in the library path. When attempting to use what I would think are the air equivalents, the application simply closes without restarting.

    import adobe.utils.ProductManager;

    var mgr:ProductManager =
    new ProductManager(“airappinstaller”);

    mgr.launch(“-launch ” +
    NativeApplication.nativeApplication.applicationID + ” ” +
    NativeApplication.nativeApplication.publisherID);

    stage.nativeWindow.close();

  5. Hey Lee,

    Did you make sure that the “allowBrowserInvocation” option is set to true in the AIR application descriptor template. This is the xml file that describes the version, name, … of your application. Because otherwise it will not work.

    It also doesn’t work in debug when you run it for example from Flex Builder. It only works if you installed the app. In debug the application that executes your AIR program is adl.exe.

    Hope this helps,
    Alain

  6. Hey Alain,

    Yes, allowBrowserInvocation is set to true in the descriptor XML, I even checked the application’s Package Contents to verify, and I’ve been testing with the installed app. I’ve found the swc that contains the mx.core.WindowedApplication class, (it seems to be the air/airframework.swc in the flex SDK), unfortunately I haven’t been able to compile from Flash CS4 with it. Dependencies seem to be in conflict.

  7. I’m on a mac, I wonder if that has anything to do with it. Which OS are you having success with?

  8. Do you know where the ProductManager class is from? I can’t find documentation of it on livedocs.

  9. I just noticed the flashlog is catching many Error #2044 when it closes.

  10. If this doesn’t work, you can always encompass all your code in an init() function and then call it

  11. Hi I’m afraid with air 2.0 it only works on osx, not anymore on windows…
    I Need real solid cross platform solution for this.

    Any clues?

    tnx,

    Arnoud


Leave a comment


No trackbacks yet.