What method to override to know when the application exits from any screen
I have to do a clean up of some app generated files, when 开发者_如何转开发the application is completely closed. The close can happen from any screen. Which method or where should i override globally to caputure Menu Close event, rather than overriding onClose() on each screen of the application ?
And when the application is closed using Menu Close in the middle of the application, is onClose() called for each screen on the stack and are popped off the stack, or it just removes the application from memory ?
I don't believe there's a method that does exactly what you want but I think you can get the behavior you want. First off, based on the testing I've done the close menu item simply calls onClose() for the current screen. The default close menu item does not close the entire application, it just closes one screen.
The closest method I can think of is deactivate(), this is called when the app is sent to the background but not when it's actually closed (i.e. this method will be called if you press the red "end call" button but not if you press close in the menu). This would probably be overkill but what you could do is select "Auto-run on startup" and "Do not display the application icon on the BlackBerry home screen" in your BlackBerry App Descriptor. This would make the app invisible to the user so that it's always in the background, to have an icon on the homescreen and display a GUI you would create an alternate entry point that will bring up the UI. Then when the user selects the close menu item all it's really doing is sending the application to the background and you can put your cleanup code in deactivate().
A much better approach would be to just override onClose() in a parent class and then just make all of your screens inherit from that class. You can put your cleanup code in there. Or if you want the close menu item to close all screens override the makeMenu() method and add a MenuItem that will execute the appropriate cleanup code before calling System.exit().
In my app, I just have all the screens inherit from a common parent class. In that parent class, I implement my standard exit handling.
The correct place to put code that runs when a screen is popped is Screen.onUiEngineAttached(boolean). That method gets called when a screen is actually pushed or popped from the display stack. The other methods are only relevant if you are overriding the behavior of menu items or dirty screen handling.
Another option would be to have a single listener object that handles all this behavior, and use Screen.addScreenUiEngineAttachedListener() to subscribe it to all screens before pushing them on the display stack.
精彩评论