What to do when your AIR to Android app closes

Hey gang, so here’s some tips and code for what to do when your Air to Android App closes. Actually lemme be a bit more specific there , because coming from the iOs world to my Android tablet, I just figured backing out of the app (hitting Back or the Home button) would kill it. Not so in the Android universe, the app isn’t actually closed (or quit) and I didn’t realize that until I had some background audio running in my Flash-made game that was still audible when the app was running in the background. And audio is just one of the things you could deal with when your app isn’t in the foreground. You probably should remove any enterframe event listeners, or stop any timers that are running. Basically you just want to get your app into a mode thats processing as little as possible.  And I’ll post up the code I used in my app, right here.  Thats a link to an Actionscript file, but I’ll paste the same code in this article, but just be aware that WordPress sometimes does funky things with the quotemarks so a straight copy and paste from the blog to your editor might cause problems. And yeeeaah, in my example file, I probably should have stopped the audio entirely, but I just turned the volume off. Not sure if there’s a huge difference anyway, and I didn’t feel like testing much to find out =)

One thing to note too, posting your app to the Google Android Market store is like the wild west of app stores. Anything goes. The app will be live within minutes and there’s no review from Google prior to posting. So they don’t really care if you handle closing/pausing your app correctly. BUT I submitted one of my games to Amazon App Store and they were more particular about what the app did in the background. Basically they didn’t want it to just quit entirely if a phone call interrupted the game. And I submitted this game months before I knew any better so at the time, my code for handling the Home or Back buttons just quit out the app entirely. They didn’t like that. So Amazon rejected it for not pausing on deactivation.

Finally, there is code for quitting your app, and thats using a button with the instance name quitBtn.

Read on, for the example code.

import flash.events.MouseEvent;

	NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate, false, 0, true);
	NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true);
	NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);

function handleActivate(event:Event):void
	NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;

		restartMyGameFromPause() ;  // see the function below


 function handleDeactivate(event:Event):void



 function handleKeys(event:KeyboardEvent):void
	if(event.keyCode == Keyboard.BACK) {




quitBtn.addEventListener (MouseEvent.CLICK, quitApp);

function quitApp(event:MouseEvent):void {



function restartMyGameFromPause()  {

	this.addEventListener (Event.ENTER_FRAME, doConstantly); // add back any enter frame listeners 

		oneSecTimer.start(); //restart the clock timer (my game had a timer in it, yours might not )

		if ( soundEnabled == true) {   // if sounds are enabled in the game, set the volume back up

				newSetting.volume = .5; // set the volume back to whatever, in this case it was 50% 
				channel.soundTransform = newSetting;



function pauseTheGame()  {

		this.removeEventListener (Event.ENTER_FRAME, doConstantly); // remove any enter frame listeners so they aren't processing in the background

		newSetting.volume = 0;  //turn off the volume for any sounds. 
		channel.soundTransform = newSetting;

		oneSecTimer.stop(); // stop a timer you might have running




4 thoughts on “What to do when your AIR to Android app closes

  1. Hi, I’m really glad I’ve found your article – you seem to be the only person who’s talking about this. My app keeps getting rejected because of call interrupts not cutting off the sound. I try to publish your code, but I get the same errors as the guy above. Are you using class that goes with it? I’m a designer, so I’m not great with code, but without your help I’ll have to give up on Amazon! I also tried the setting ‘READ PHONE STATE’ in Flash 5.5, but that didn’t work with Amazon.. Thanks, Matt

  2. Hello,
    Thank you for your article. I am having the same issue with closing or quitting my apps. Do you post the code inside of flash on the time line or in the seperate xml file?
    Thank you,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s