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:
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:
{
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:
So this was actually what I needed, and it worked!
Any feedback is appreciated
18 Responses to How to restart an AIR application from code
Leave a Reply Cancel reply
Lifestream
-
Checking out CocoaPods - an Objective-C library manager > https://t.co/xdLU5OgO— February 1st via Twitter
-
Finally found some time to dive into iOS5 Storyboarding.— February 1st via Twitter
-
RT @robbedoes: Fun to watch: an interview w/ the sound designer of Angry Birds: http://t.co/lDARIGbB— February 1st via Twitter
-
@wuotr Also, check this out: Flood Fill > http://t.co/SpweLK0N ;)— January 30th via Twitter
-
Love it! Timesaver++ CocosBuilder, a tool for graphically laying out sprites, layers and scenes for @cocos2d > http://t.co/Jrb8233D— January 30th via Twitter
-
RT @rwenderlich: Awesome list of Cocos2D source code projects, extensions and code from @iuridium: http://t.co/ovwLYhCv— January 29th via Twitter
-
Moo! @ Kinderboerderij kiewit http://t.co/tYBlLsvH— January 29th via Twitter
-
I've been doing my epic chores and just leveled-up. I'm a Level 8 Well-groomed Clansman now! #EpicWinApp— January 27th via Twitter
-





Fantastic!
Thank you so much.
Adam McCrossan
Is, “var app:WindowedApplication = WindowedApplication(Application.application)” supposed to be, ” var app:WindowedApplication = WindowedApplication(NativeApplication.nativeApplication)”?
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.
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();
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
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.
I’m on a mac, I wonder if that has anything to do with it. Which OS are you having success with?
Do you know where the ProductManager class is from? I can’t find documentation of it on livedocs.
I just noticed the flashlog is catching many Error #2044 when it closes.
If this doesn’t work, you can always encompass all your code in an init() function and then call it
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
Excelente Maestro!!! Funcionó de peluches!!!
Gracias por el truco.
[...] 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 [...]
It works! I use Windows 7.
Thank you so much!
I used ”
var app:WindowedApplication =
WindowedApplication(FlexGlobals.topLevelApplication);”
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.
A cool blog post there mate . Thanks for it !
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
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