views:

359

answers:

4

What's the best way to close resources in a BlackBerry application? I use the resources throughout the life of the application and would just like to register some listener to be called when the app is just about to quit. Is there a simple hook that's called before the app is closed?

A: 

WHhen your app quits all resources are reclaimed by the OS.

You definitely have the right mindset that freeing up resources is a good thing to do, but if you have to jump through hoops to do so when your app quits then it may not be worthwhile.

Andrew Grant
A: 

One point as Andrew mentioned should be thought out. If you have to jump through hoops to free resources, try to change the design so as to ensure that resources are freed through one control point. This will make it easier for you. and more maintainable.

omermuhammed
+1  A: 

One thing you can do is to trap for the ESC key. When only your Main Screen is in the screen stack ( UiApplication.getScreenCount() ) & the ESC key is pressed, you can then clean up your resources.

  1. Also, you can register a SystemListener to listen for powerOff() callbacks to kill threads or clean up resources.
  2. Furthermore, UIApplication.deactivate() is also a good place to clean up/suspend stuff when your app is backgrounded.
Jacques René Mesrine
+1  A: 

Overwrite the MainScreen.close() function of the screen that is the last to be popped out of the UI stack. This close() method is most probably the last method that is called before your application is destroyed.

Let me please add, that it is highly recommended to always close streams especially for file access.

kozen