The BlackBerry Application Life Cycle BLACKBERRY

BlackBerry applications behave very much like ordinary desktop applications. If you’ve had experience with C, C++, or Java on the desktop, some of the ideas in the previous probably seemed very familiar to you. Specifically, the main method for a BlackBerry application is identical to the main method for a Java SE application (which is very similar to C’s). While there are exceptions for specific needs, almost all BlackBerry applications will follow the same life cycle. In this book, all of our applications will fundamentally look like the Hello World application we created in the last chapter.

Starting the Application

An application is generally started in one of three ways:

  • The user clicks the application’s icon on the BlackBerry home screen.
  • The application is an automatically starting application and runs when the device is turned on or after it reboots.
  • The application is run by another application.

In all cases, the main method is the first entry point for your application. The BlackBerry device will create a process, which will call that method. Whenever the main method exits, the process is terminated and your application exits. This means that if you want your application to do anything, you’d better do it in that main method.The main method takes an array of java.lang.String objects as parameters. For the most part, this array is empty, but parameters can be passed in if you define them in the project properties or if they’re passed by another process that is starting your application.

Creating the Application

All BlackBerry applications that want to present a user interface to the user must extend UiApplication. You can only create one instance of UiApplication for any application process; the BlackBerry runtime will throw an exception if you try to instantiate a second one. Even applications with no user interface must extend net.rim.device.api.system.Application, but those types of applications are outside the scope of this. You can always access your application instance using the static method UiApplication.getUiApplication(). This actually returns an instance of your application class, so from anywhere in Hello World, the following is allowed:

HelloWorldApp helloWorld = (HelloWorldApp)UiApplication.getUiApplication();

Invoking the Event Thread

The event thread is started for you by the BlackBerry operating system, but it doesn’t start processing events and drawing the UI until you explicitly tell it to. You do this with the UiApplication.enterEventDispatcher() call that you saw in the last chapter. Once this method is called, the thread that entered into the main method passes from your direct control and takes up the task of listening for user interface input and drawing the user interface to the screen. You’ll still get a chance to do work on the thread, but for the most part, its activities are scheduled by the BlackBerry operating system. enter Event Dispatcher won’t return for the entire life cycle of your application, so if there’s anything your main thread must do before calling this (for example, some types of initialization) you have only one chance.

Processing Events

The application responds to keyboard input, trackball, or touch screen movements and clicks and to other events like system messages.

Exiting the Application

Generally, a BlackBerry application exits when the last screen is removed from the display stack (by closing it). You may have noticed the System.exit() method, which will exit the application, but it’s recommended to avoid this and properly clean up the application on exiting by closing all screens instead. When the application exits, all application state will be cleaned up, and the next time the user clicks the application icon the main method will be called again with a new process.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status