The BlackBerry Red Key!

When I’m running an application on a BlackBerry device — and I hit the red key — my app disappears, and the menu screen reappears. I’d guess that my app’s screen was popped off the BlackBerry screen stack, so onClose() was called and my app cleaned itself up neatly, right?

Wrong.  The application was merely sent to the background.

I assume this behavior is mentioned somewhere in the bowels of the RIM documentation (although I’ve read quite a lot of it, and I haven’t seen mention of this). Nor do the sample applications appear to deal with it. I just figured it out when my colleague Andrew Davison sent me a game that plays music in the background. It was a tad loud, so I instinctively tried to shut it off in one click using the red key. And the game went away, but the music kept right on playing…

It reminds me of a little something I read in the PodTrapper story:

My curve has 32MB of RAM and 64MB of flash. The flash can be used for paging if it’s available, but on my device only about 20MB of it is, and that fills up fast installing applications (PodTrapper alone is 350k). Realizing that the OS and other apps also have to fit in RAM, leaking a 100kb string becomes a huge problem. There are lots of forum posts by people trying to figure out why their phone keeps running out of memory and needs to be rebooted. It’s a large enough problem that there are apps for automatically restarting your phone. I can definitely see why Apple has been hesitant to open up background processing on the iPhone. It’s really easy for bad developers to make the whole platform look bad.

Personally, I think it’s kind of cool that you can run apps in the background on BlackBerry, and send them to the foreground and back. However, I think the simplest, default behavior should lean towards cleaning up the app neatly, while sitting on memory and processes when your app goes out of scope should be a special behavior that advanced developers can request.  Not the other way around.

So what about that red key?  What if you’re developing a game or animation that makes no sense in the background, and you just want it to clean up whenever the user dismisses it?

Andrew proposed the following solution (for use in a subclass of Screen):

// keycodes for the green and red keys
private static final int GREEN_KEY = 1114112;
private static final int RED_KEY = 1179648;

protected boolean keyDown(int keycode, int time)
// terminate the game if the green or red keys are pressed
{
// System.out.println(“keyDown: ” + keycode);
if ((keycode == GREEN_KEY) || (keycode == RED_KEY)) {
isRunning = false;
return true;
}
return false;
}  // end of keyDown()

***

That works, but on some level I couldn’t help but feel like it’s absurd of RIM to expect developers to check for the key codes of the red key and green key instead of providing some sort of help.  This has got to be a fairly common scenario, doesn’t it?

Poking around the API a bit, I found it:  In your Application subclass, you implement deactivate().  Here’s the JavaDoc:

public void deactivate()
Handles backgrounding event.
The system invokes this method when sending this application to the background. By default, this method does nothing. Override this method to perform additional processing when being sent to the background.

So, if you’re writing an application that makes no sense in the background, then you implement this method and have it perform the app’s cleanup and shutdown.

Like so many aspects of BlackBerry programming, it’s quite simple once you notice the problem and figure out the trick. ;)

About these ads

2 comments so far

  1. Wojciech on

    What about the situation like incoming call, text message, low batery pop up etc.? Using deactivate method and placing cleanup there would close the app in those situations as well and that wouldnt be simplest expected behaviour

  2. sahil on

    Thanks a lotzzz ur fix helped me in controlling game sound..which was playing in background by pressing red key… ;-)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: