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 :)

Tagged with:
 

18 Responses to How to restart an AIR application from code

  1. Fantastic!

    Thank you so much.

    Adam McCrossan

  2. lee says:

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

  3. Alain says:

    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. lee says:

    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. Alain says:

    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. lee says:

    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. lee says:

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

  8. lee says:

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

  9. lee says:

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

  10. Patrick Gates says:

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

  11. Aanoud says:

    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

  12. Asuej28 says:

    Excelente Maestro!!! Funcionó de peluches!!!

    Gracias por el truco.

  13. [...] solutions on the web but they don’t work an all platforms! For example the solution presented here is not working on windows but worked on the mac. We don’t need hacks but an Adobe [...]

  14. cool says:

    It works! I use Windows 7.
    Thank you so much!
    I used ”
    var app:WindowedApplication =
    WindowedApplication(FlexGlobals.topLevelApplication);”

  15. cbrown says:

    Works for me on both osx 10.6.8 and windows 7 with an air app from flash pro using:

    import flash.desktop.NativeApplication;
    var mgr:ProductManager = new ProductManager(“airappinstaller”);
    mgr.launch(“-launch ” + NativeApplication.nativeApplication.applicationID + ” ” + NativeApplication.nativeApplication.publisherID);
    NativeApplication.nativeApplication.exit();

    UNLESS… an flvplayback component loads a video! After that, it will only close and not restart. I’ve tried everything under the sun to destroy the video/component before the restart to no avail. Any suggestions are very appreciated, thanks.

  16. Ceola Phomphithak says:

    A cool blog post there mate . Thanks for it !

  17. SnakeMedia says:

    WOW that is a nice trick!
    For Windows Vista and 7 Version?
    Eh that is best idea but how does Adobe AIr work when i am starting into Administrative Right? If Air App writes regfile and starts process with powershell for regfile will write into registry database than Air App will restart – but it can not show Administrative Mode ( Yes or No ).

    Thanks best regards SnakeMedia

  18. SnakeMedia says:

    ANd i have been fixed for Flex 4.6 SDK

    Please do not forget for Application into FlexGlobals replacement!!!!

    Look like this:
    Code:

    /**
     *
     * Reboot of Air App
     *
     */
    package
    {
      import adobe.utils.ProductManager;
     
      import flash.desktop.NativeApplication;
     
      import mx.core.FlexGlobals;
      import spark.components.WindowedApplication;

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

    If you are using Flex 4.6 than you just replace from “Application” to “FlexGlobals” It will work for restart. I have been testet this Application Air :)

    Thanks SnakeMedia

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.