Fully Exiting VS. Running in the Background
Blackberry applications will fully exit when the last screen is closed. This can be very costly since it requires the entire application to be re-initialized every time a user accesses it. A common practice for Blackberry applications is to send an Application to the background instead of allowing it to close when a user closes the last screen. In fact, many Blackberry applications only re-open when the device is rebooted. This can be done simply by calling:
UiApplication.getUiApplication().requestBackground();
This is typically implemented by overriding the onClose() method of the application's main screen as shown here:
public boolean onClose() { UiApplication.getUiApplication().requestBackground(); return false; }
| Metova's SDK Tip With Metova's framework it is even easier to implement by calling setBackgroundOnClose(true) in the constructor of your main container. |
After adding the above method, the application will not exit when the user attempts to close the screen (assuming it is the last screen). Instead, the application will be sent to the background, and will return to that screen immediately when re-opened.
requestBackground() will also produce the expected result when called from another location such as a menu item.
If you still need your application to fully exit, an easy way is to add an exit or logout menu item. To do this override the method:
protected final void makeMenu( Menu menu, int instance )
And add a menu item:
menu.add( Menus.newMenuItem( "Exit", new Runnable() { public void run() { ((com.sample.bb.MyApplication)Application.getApplication()).onExit(); } } ));
This code called the onExit method of the main application which is a good place to cleanup any resources and exit your application with a System.exit(0).
public void onExit() { Preferences preferences = PreferenceStore.instance().load(); preferences.setUsername(null); preferences.setPassword(null); PreferenceStore.instance().commit(); System.exit( 0 ); }
