Responding to activation and deactivation

The BlackBerry 10 OS can deactivate your application or move it to the background at any time. When your application is deactivated, you should save the application state. You can also stop any unnecessary processes (such as updating the UI) to preserve system resources. When your application is activated again, you can load the saved state and start any suspended  processes.

You should not assume that BlackBerry device users save their activity in your application before your application moves to the background, or is deactivated.

By default, the EventListener object dispatches one of the following events when your application changes its state in the life cycle:

Event

Description

Event.DEACTIVATE

Dispatched when another application becomes the active application.

Event.ACTIVATE

Dispatched when the application becomes the active application.

Listen for application activation or deactivation

How to

Listen for the activation or deactivation of your application.

Solution

Set up event listeners to listen for the Event.DEACTIVATE and Event.ACTIVATE events in the root of your class.

addEventListener(Event.DEACTIVATE, onDeactivate);
addEventListener(Event.ACTIVATE, onActivate);

Define an onDeactivate function that is called when the application is deactivated and loses focus.

public function onDeactivate(event:Event):void 
{
   trace("Screen Deactivated");
   // Save state 
   // Suspend processes   
}

Define an onActivate function that is called when the application is activated and has focus.

public function onActivate(event:Event):void 
{
   trace("Screen Activated");
}

Build requirements

import flash.events.*;

Discussion

Use the onDeactivate function to save the state of the application and stop any unnecessary processes.

Use the onActivate function to save or load the state of the application and start any processes that were stopped.

You can use trace comments to test and troubleshoot your application. The debug console in Adobe Flash Builder or Powerflasher FDT displays trace comments when testing your application in the debug configuration.

comments powered by Disqus