Add a Menu Item to a Native Application
Create the service class
Create an instance of NativeMenus that extends org.metova.bb.widgets.menu.NativeMenus. In this new instance add the function:
public static void initializeNativeMenus( Application application ) { int processId = MobileApplication.instance().getProcessId(); // initialize menu items here addNativeMenus( application ); }
Create the menu listener
Your menu listener will extend the class NativeMenuListener and look something like:
public class ComposeMenuListener extends NativeMenuListener { public static final long GUID = ComposeMenuListener.class.getName().hashCode(); public Event getEvent( Object context ) { return new ComposeMenuEvent( context ); } public Class getEventClass() { return ComposeMenuEvent.class; } public long getGuid() { return GUID; } public void onEvent( final Event event ) { // do something } }
Create the menu item
Your menu item will extend the class NativeMenuItem and look something like:
public class ComposeMenuItem extends NativeMenuItem { public ComposeMenuItem(int processId, long type) { super( processId, type, "Add Contact", new ComposeMenuListener() ); } }
Register the menu item and listener
Registering the listeners is done in two places. First you will want to add it to the NativeMenus service class you created earlier. From the NativeMenus.initializeNativeMenus() function call the helper methods you have created:
public static void initializeNativeMenus( Application application ) { int processId = MobileApplication.instance().getProcessId(); // initialize menu items here initializeComposeMenu( processId ); addNativeMenus( application ); } private static void initializeComposeMenu( int processId ) { long menuItemType = ApplicationMenuItemRepository.MENUITEM_EMAIL_EDIT; ComposeMenuItem menuItem = new ComposeMenuItem( processId, menuItemType ); NativeMenuHolder.instance().addElement( menuItem ); }
In your Application class, under Application.lifecycleParallelPostStartup() call the service class.
protected void lifecycleParallelPostStartup() { super.lifecycleParallelPostStartup(); NativeMenus.initializeNativeMenus( this ); }
Unregister the menu items when exiting your application
The ManagedUiApplication class will handle unregistering any menu items that were added through the NativeMenus service.
