Handling Menu Commands
In order for the menu to be useful, you need to handle its commands. Where you handle commands depends on the type of object containing the menu. In a CAknAppUi- or CAknView-derived class, you use the HandleCommandL() method. In CAknDialog-derived classes, you use the ProcessCommandL() method.
Objects that handle commands inherit from the MEikCommandObserver mixin. This defines a pure virtual method ProcessCommandL(). When you select a menu item, the framework calls ProcessCommandL(). CAknAppUi andCAknView provide a default implementation of ProcessCommandLQ, which callsHandleCommandLQ.
As the SimpleMenu example uses the menu in the AppUi, the HandleCommandL() method is overridden in the SimpleMenuAppUi class:
void CSimpleMenuAppUi::HandleCommandL(Tlnt aCommand)
switch (aCommand)
case ESimpleMenuCmdViaIR: ¡AppContainer->PlayNewGameVia(aCommand);
break;
case ESimpleMenuCmdViaBluetooth: ¡AppContainer->PlayNewGameVia(aCommand); break;
case ESimpleMenuCmdViaSMS: ¡AppContainer->PlayNewGameVia(aCommand); break; case EAknSoftkeyBack: case EEikCmdExit: ExitO; break; default: break;
This code shows a standard way of handling commands—a switch statement handles each command ID on a case-by-case basis. In the SimpleMenu example application, handling must be provided for the commands in the submenu and the exit command. The new game command does not involve extra handling code—the framework takes care of this, popping up the submenu when you select the New Game item.
You should handle the exit command by calling the Exit() method, which closes the application. You should handle the Back soft key in the same way. You may have noticed that the Exit command handled above (EEikCmdExit) is not the command associated with the Exit menu item (EAknCmdExit). There are two predefined commands for exiting an application: EAknCmdExit (in avkon.hrh), and EEikCmdExit (in uikon.hrh).
When defining exit menu items in the application resource file, you should use the EAknCmdExit version. For example:
MENU ITEM
command = EAknCmdExit; txt = EXIT_TEXT;
However, when handling the command in ProcessCommandl_(), or HandleCommandL(), you should always handle EEikCmdExit. For example:
void CSimpleMenuAppUi::HandleCommandL(Tlnt aCommand) switch (aCommand)
case EAknSoftkeyBack:
case EEikCmdExit: {
ExitO;
This is because, when EAknCmdExit activates, the application framework needs to close the current application and any related applications. The term "related applications" refers to situations where another application either embeds the current application or contains embedded applications. All closeable applications are required to handle the EEikCmdExit command by calling CEikAppUi::Exit(), which closes the application. To ensure that all related applications are closed, the framework sends an EEikCmdExit to each of the related applications, and expects them to handle the command by calling the Exit() method.
Note that the EEikCmdExit command is reserved for system use (such as Out-Of-Memory watchdogs), and so must call Exit() without querying the user. This is covered in more detail in the Good Application Behavior section of Chapter 4.
The Back soft key will always activate the EAknSoftkeyBack command. The behavior invoked by the Back soft key depends on the screen currently being displayed. Screens that are not at the top level should handle the back command by returning to the previous screen.
The top-level screen should handle the back command by exiting the application in the same way as the exit command does. You can see this in the HandleCommandl_() code above, which handles both the EAknSoftKeyBack and EEikCmdExit commands in the same way.
NOTE
When you configure the soft keys such that the right key is Exit rather than Back (R_AVKON_SOFTKEYS_EXIT), the command that should be specified and handled is EAknSoftkeyExit.
Post a comment