BlackBerry: Duplicate application menu items in UiApplication
I have a BlackBerry UiApplication, which registers some menu items in the standard Phone and Contacts applications. I need the menu items to be registered on phone startup, ie, before my UiApplication is started.
I can achieve this if I configure my UiApplication to auto-run on startup, and register the menu items in my app initialisation code using ApplicationMenuItemRepository.
My problem is that every time my UiApplication is subsequently opened, my initialisation code is run again, and I get duplicate menu items in the Phone and Contacts app. ApplicationMenuItemReposi开发者_开发问答tory does not provide an API to check if they are already registered. Using a static boolean in my own code also does not help, presumably because different classloaders are used for each app instance.
Am I using the wrong approach here? Should I have a separate Application (to register Phone/Contacts menu items) and UiApplication (for my views)? That feels overly complex for my needs.
Use the Alternate Entry Point
Click on the project node.
Right click and select Properties.
In the Properties window, select the Application tab.
Ensure the following options are checked: Auto-run on startup and System module (to register the thread with the system).
Create another project under the same folder as the original project. Right click on the new project node and select Properties.
Select the Application tab and select Alternate CLDC Application Entry Point from the Project type drop down menu. As shown in the attached file, select the name of the original project (for example: trafficreporter) from the Alternate entry point for drop down menu. Also specify the arguments that would launch the application using this alternate entry point (for example: gui).
Modify the main() method of the original project as follows:
public static void main(String[] args) { if ( args != null && args.length > 0 && args[0].equals("gui") ){ // code to initialize the app theApp.enterEventDispatcher(); } else { // code to launch the background thread } } }
Add your application iconfile to the this new "Entry Point" application and make it the ribbon icon.
Use the removeMenuItem() method when the user exits your application. it will work.
if(_serverMenuItem != null) {
ApplicationMenuItemRepository.getInstance().
removeMenuItem(ApplicationMenuItemRepository.MENUITEM_PHONE,_serverMenuItem);
}
If you want to add custom menu fields to native applications you can use a RunTime store to register you menu item and then check for it while you re-run your code :
ApplicationMenuItem ami = new ApplicationMenuItem(placement); // some placement you want to use e.g 0x35090
ApplicationMenuItemRepository amir = ApplicationMenuItemRepository.getInstance();
RuntimeStore store = RuntimeStore.getRuntimeStore(); // get the store instance
if(store.get(ApplicationMenuItemRepository.MENUITEM_CALENDAR) == null)// if object is not added only then add the item to the menu
{
try
{
store.put( ApplicationMenuItemRepository.MENUITEM_CALENDAR, ami );
}
catch(IllegalArgumentException e){}
amir.addMenuItem(ApplicationMenuItemRepository.MENUITEM_CALENDAR, ami);
}
精彩评论