The app may enter an abnormal state and behave in a way that a user does not expect. You need to manage these system actions so the app is ready for any transition at all times.
There are special circumstances where the Blackberry 10 OS will act on the app without any user interaction. For example, when the device runs out of battery and shuts down, it immediately terminates all apps on the device, regardless of the run partition. Your app must be ready to handle any type of condition.
It is important to make sure that the app, when terminated abruptly without any input from the user, properly saves its states and preserves its data without any losses. For detailed instruction on saving the app state, see Saving the App State.
Low-memory conditions can occur when the user has many memory intensive apps open and running in the background. Alternatively, apps may hold memory unnecessarily if they do not suspend processes when moved to the background. When the system runs low on memory, the BlackBerry 10 OS attempts to reclaim resources by closing background apps. The OS attempts to close the app that has been in the background the longest, and continues to close apps until it reclaims enough memory for normal system operation.
When you receive a low-memory event, your app should immediately save the state, release resources, and destroy its objects. If these steps are done correctly, the user's app data will be preserved if your app is terminated. If your app receives focus again, the user expects to return to the app in the state it was in when it was pushed to the background. Your app should detect whether it was terminated due to low memory, then load the saved state if appropriate.
The following code example shows how to detect a low-memory event using the BlackBerry Platform Services library:
// 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);
if (event) {
int domain = bps_event_get_domain(event);
if (domain == navigator_get_domain()) {
switch (bps_event_get_code(event)) {
case NAVIGATOR_LOW_MEMORY:
// Perform low memory handling steps here
break;
}
}
} else {
break;
}
}
If the battery of the device drains completely, the device closes all apps and shuts itself down until the battery recharges. Your app can receive notification about changes in the battery level and state, so you can evaluate the remaining battery and respond appropriately by saving the app state and warning the user, before the system shuts down.
For more information on responding to system events, see BlackBerry Platform Services.