Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Posts: 298
Registered: ‎05-02-2010
My Device: Bold 9700
My Carrier: Verizon
Accepted Solution

A question on how to properly handle a single Timer on a screen, when to schedule and cancel?

[ Edited ]

A design question, what is a good implementation of the following?


I have a single class that extends MainScreen. On that class I have a single Timer and inner class that extends TimerTask.



public class MyScreen extends MainScreen {

  private Timer _t;

  private class MyTask extends TimerTask{
    public void run() {
    ...impress your girlfriend with cool logic...



  • What is the best place to schedule the Timer to run the timer each time the screen is loaded?
    1. Constructor
    2. ScreenUiEngineAttachedListener
  • What is the best place to cancel the Timer to ensure that is truly cancelled once the user has either closed the screen or press the physical End button on the device?
    1. ScreenUiEngineAttachedListener
    2. onClose
    3. keyDown
    4. ...or all of these?


Is there a best practice to use?



Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: A question on how to properly hand a single Timer on a screen, when to schedule and cancel?

Don't start and cancel the Timer - create, schedule and cancel TimerTasks!


Have a static private Timer _t = new Timer(); - this way you'll have just one per application (at least for this class), statically initialized. Cancel that timer just before you shut down the application. Schedule all your TimerTasks on that one timer.


Your question becomes then "when do I schedule and cancel my TimerTasks"? It depends on your exact requirements for your TimerTasks, but the good places would be:


To schedule:

Screen.onDisplay() / onUiEngineAttached(true)

Screen.onExposed() (if you go the onObscured() route below)

Screen.keyChar() / keyDown() / keyControl() (if you go with the DeviceInfo.getIdleTime() check below)

Screen.navigationClick() (see above)

Screen.touchEvent (see above, for touchscreen devices only)


To cancel:

Screen.onUndisplay() / onUiEngineAttached(false)

Screen.onObscured() (pairs with onExposed() above)

if (DeviceInfo.getIdleTime() > yourTimeoutValue inside) TimerTask.run() method - need to also process the aforementioned keyChar/keyDown, navigationClick() and touchEvent() then.


One thing to keep in mind - an actively running TimerTask will drain your battery quite quickly by preventing your phone from going into a low-power mode, so be sure to cancel it when you don't need it. An "active" Timer without any tasks scheduled on it, on the other hand, will just sleep forever, so don't bother killing/recreating it.

please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!