Handle Window Server-Generated Events
There are several different types of Window Server events, such as key events, system events and foreground events, which must be handled appropriately. The AppUi class has several functions that are called by the Window Server to handle each type of event notification—these were previously overviewed in the Important AppUi Methods subsection.
What you do within these overridden functions is up to you, but the following discussion details some likely scenarios and suggests ways to handle events responsibly.
It is possible for an application to gain or lose focus at any time. This can happen if the user switches between applications, or if the system brings another application into the foreground. Whenever an application's window group gains or loses focus, the application UI's HandleForegroundEventL() method will be called. This method has a single TBool parameter, which will be ETRue if the application is gaining focus and EFalse if it is losing focus.
When an application loses the foreground, it must strive to establish a stable, recoverable state. This means suspending all processing and saving application data—for two reasons. First, processing power must be reserved for the foreground application. It is of primary importance, because it is the application with which the user interacts. If background applications were to continue processing, users might experience an unwanted reduction in performance. Second, background-running applications may be automatically shutdown by the system in low memory conditions. If a forced shutdown were suddenly to occur, and background applications were to continue processing, it might not be possible to establish a steady and recoverable state.
User input must also be handled correctly. When the user carries out an action on the user interface, an event will be sent to the application that owns the window the event occurred in. There are several different types of events, but you will be interested mainly in key events and redraw events. Good programming behavior recommends that you take into consideration not only the key presses you expect, but also those that you do not.