Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Delay Closing Mobile Apps on Exit

DZone's Guide to

Delay Closing Mobile Apps on Exit

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

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"
creationComplete="init(event)"
xmlns:s="library://ns.adobe.com/flex/spark" >
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
public var closeTimer:Timer;
protected function init(event:FlexEvent):void
{
NativeApplication.nativeApplication.addEventListener(
Event.DEACTIVATE, onDeactivate);
NativeApplication.nativeApplication.addEventListener(
Event.ACTIVATE, onActivate);
}
protected function onDeactivate(event:Event):void
{
closeTimer = new Timer(30000,1);
closeTimer.addEventListener(TimerEvent.TIMER_COMPLETE,
closeApp);
closeTimer.start();
}
protected function closeApp(event:Event):void{
NativeApplication.nativeApplication.exit();
}
protected function onActivate(event:Event):void
{
closeTimer = null;
}
]]>
</fx:Script>




</s:Application>

 

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:

Published at DZone with permission of Terrence Ryan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}