Wednesday, May 4, 2011

Responding to application deactivation and activation for Blackberry


Responding to application deactivation and activation

The BlackBerry® Tablet OS is a multitasking operating system, which means that your application can be deactivated and moved to the background at any time. Sometimes, this is user initiated, such as when the user actively leaves your application to open another application. However, a loss of focus can also be initiated by the system, or by another application. In any case, you cannot assume that the user has appropriately saved their activity in your application before it moves to the background.
When your application is deactivated and pushed to the background, it is important that it first saves its state. It is also important that your application stop any unnecessary processes to preserve system resources, to avoid needlessly using system resources and risking low memory conditions. When the application is activated and returns to the foreground, it can reload the saved state, and restart any stopped processes.
Each time your application transitions from the foreground to the background and back again, theNativeApplication object, which represents the application for the duration of the life cycle and which is instantiated automatically when the application is invoked, dispatches one of the following events:

Property
Description
Event.DEACTIVATE
Dispatched when the application is pushed to the background.
Event.ACTIVATE
Dispatched when the application becomes the active application.

Respond to deactivation and activation

  1.  Import therequired packages.
    import fl.events.*;
  2.  Set up event listeners to listen for the Event.DEACTIVATE and Event.ACTIVATE events.
    addEventListener(Event.DEACTIVATE, onDeactivate);
    addEventListener(Event.ACTIVATE, onActivate);
  3.  Define the callback function called when the application loses focus. This function should save the state of the application and stop any unnecessary processes.
    public function onDeactivate(event:Event):void
    {
        // Save state
        // Suspend processes
    }
  4.  Define the callback function called when the application regains focus. This function should save the reload the state of the application and restarted any processes that were stopped.
    public function onActivate(event:Event):void
    {
        // Reload state
        // Restart processes
    }

No comments:

Post a Comment