App Deactivation/Activation

The most basic states that an app transitions back and forth are the active and the inactive state. You need to manage the activation/deactivation of the app to make it a fully functional app.

There are many states and partitions that an app can pass through during its life cycle. However, the most basic yet necessary transition that must be dealt with is the Activation and Deactivation transition. By properly managing your app’s activation/deactivation transition, you can deal with the majority of the transitions with little problem.

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

When the OS deactivates your app, your code should first save the app state. In addition, your app should stop any unnecessary threads and processes (such as updating the UI in real time) to preserve system resources. When the OS activates your app again, you can reload the saved state, and restart any suspended processes. For more information, see Saving the App State page

You can detect app activation and deactivation events by using the BlackBerry Platform Services library. The following code examples illustrate how an app can initialize and handle the NAVIGATOR_WINDOW_ACTIVE event, which indicates that the app window has changed from hidden to full screen, and the NAVIGATOR_WINDOW_INACTIVE event, which indicates that the app 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 app deactivation steps here
          break;

     case NAVIGATOR_WINDOW_ACTIVE:

          // Perform app activation steps here
          break;
     }
}