by fjenning

 Comments (7)

Created

April 20, 2011



When you are building AIR-based mobile applications, you can handle Tombstoning in your application in a very elegant way. Tombstoning is a technique by which you freeze the state of your application whenever a system interrupt occurs.  For instance, when the user receives a cellular call, your application will become invisible and may get deactivated or pushed to the background. During that time, there is no point in running those game timers or animations. You can suspend all CPU/GPU intensive activities and resume all of them when the application gets activated again.

Tombstoning is an absolutely required feature that your application should support to provide a less frustrating experience to the users.

In AIR-based mobile applications, handling Tombstoning is absolutely simple.

When you are creating the mobile view, just remember to handle the view-activate and view-deactivate events:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            title="HomeView"
            viewDeactivate="handleDeactivate()" 
            viewActivate="handleActivate()">

    <fx:Script>
        <![CDATA[

            protected function handleDeactivate():void
            {
                   //Pause game timers
                   //Suspend animations
            }

            protected function handleActivate():void
            {
                   //Restart game timers
                   //Resume animations
            }

        ]]>
    </fx:Script>
</s:View>

You can also use these techniques to gracefully shutdown your application as the deactivate event is called on the View when the user taps the back key. Note: Tombstoning will not work if you are building AIR applications with 2.5 or lower namespace.


COMMENTS

  • By Adam Zucchi - 7:21 PM on May 17, 2011   Reply

    Hey,

    Thanks for the article. I typically do not work inside of Flash Builder, so would this tombstoning approach when working in AIR publishing from Flash CS5 be using the NativeApplication class and listening for the EXITING event?

    Thanks again.

    Adam

    • By fjenning - 3:18 AM on May 18, 2011   Reply

      Hey Adam,

      You can try this:

      NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onNativeAppDeactivated);

      Thus, you can handle the deactivate event.

  • By Sidney de Koning - 1:01 PM on May 20, 2011   Reply

    How would you go about this in a pure AS3 approach for Android? Event.ACTIVATE and Event.DEACTIVATE? And do you set them to stage or to NativeApplication.nativeApplication?

    Kind regards,

    Sidney de Koning

  • By fjenning - 1:10 PM on May 20, 2011   Reply

    For pure AS3, you can use:

    NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, onNativeAppActivated);

    and

    NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onNativeAppDeactivated);

    Now, on Android with AIR 2.5, this won’t work when your application is pushed to the background due to system interrupts. With AIR 2.6, it will work fine.

  • By Sidney de Koning - 1:52 PM on May 20, 2011   Reply

    So that would work if you get a call for instance?

  • By Sidney de Koning - 2:27 PM on May 20, 2011   Reply

    And why do they get called 4 time in a row? And yes i have only one listener in my entire project.

    Cheers,
    Sidney

    • By fjenning - 5:42 AM on May 21, 2011   Reply

      Sydney, Do you mean that the DEACTIVATE/ACTIVATE method is called several times when the application gets pushed to the background on Android? If you can share your code, mail your app to fjenning [AT] adobe [DOT] com

      FJ

ADD A COMMENT