Over a million developers have joined DZone.

Delay Closing Mobile Apps on Exit

DZone's Guide to

Delay Closing Mobile Apps on Exit

· Mobile Zone ·
Free Resource

When a user switches to another application on a mobile device AIR on mobile applications keep running. It continues to hold on to resources, fire off events, and has the potential to use power draining resources like network calls, or geolocation if your app makes use of them.

That's why one of the first tips you see is to kill or tombstone your application when the user switches away from it. In general, unless you have a good reason to do not do this, you should. Your users might not notice that you drain their battery to zero, but then again they might.

But I have a good reason for not wanting to do this right away. Actually I have a few:

  • As a user, I frequently accidentally hit the home instead of back button
  • As a user, I frequently lookup stuff on the web then come back to an application.
  • As a developer, I frequently makes calls to OS resources like the camera or mapping in my app.

All these add up to needing to revisit closing on exit.
I'd like to get the benefit of closing on exit in terms of resource usage, but I'd like a grace period. So, my solution, fire off an event when a user leaves the application, that sets a timer to shut down the app. Then if the user returns, kill that timer.

Simple, easy, and best of both worlds.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" >
import mx.events.FlexEvent;
public var closeTimer:Timer;
protected function init(event:FlexEvent):void
Event.DEACTIVATE, onDeactivate);
Event.ACTIVATE, onActivate);
protected function onDeactivate(event:Event):void
closeTimer = new Timer(30000,1);
protected function closeApp(event:Event):void{
protected function onActivate(event:Event):void
closeTimer = null;




Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}