Responding to app deactivation and activation

The BlackBerry Tablet OS can deactivate your application and move it to the background at any time. For example, a user may leave your application to open another application. You should not assume that the user appropriately saved their activity in your application before the application moves to the background.

When the OS deactivates your application, you should first save the application state. In addition, your application should stop any unnecessary threads and processes (such as updating the UI in real time) to preserve system resources. When the OS activates your application again, you can reload the saved state, and restart any suspended processes.

You can detect application activation and deactivation events by using the BlackBerry Platform Services library. The following code examples illustrate how an application can initialize and handle the NAVIGATOR_WINDOW_ACTIVE event, which indicates that the application window has changed from hidden to full screen, and the NAVIGATOR_WINDOW_INACTIVE event, which indicates that the application window has changed from full screen to hidden:

int rc;

// Initialize the BPS library
bps_initialize();

//Request and process available Navigator events
navigator_request_events(0)

for(;;) {
     bps_event_t *event = NULL;
     rc = bps_get_event(&event, 0);

     assert(rc == BPS_SUCCESS);

     if (event) {
          int domain = bps_event_get_domain(event);

          if (domain == navigator_get_domain()) {
               handleNavigatorEvent(event);
          }
     } else {
          break;
     }
}
static void handleNavigatorEvent(bps_event_t *event) {

     bps_event_t *activation_event = NULL;

     switch (bps_event_get_code(event)) {
     case NAVIGATOR_WINDOW_INACTIVE:

          // Perform application deactivation steps here
          break;

     case NAVIGATOR_WINDOW_ACTIVE:

          // Perform application activation steps here
          break;
     }
}