Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

New Developer
Posts: 20
Registered: ‎07-18-2008
My Device: Not Specified
Accepted Solution

Custom menu item to Message application: two issues

I am trying to add a custom menu item to the Blackberry message application, and get two issues:

  1. When the custom menu is clicked, the "Application.getApplication().requestForeground();" method call does not invoke the main() method of my application "DemoMI".
  2. If I changed the method call from
ApplicationManager.getApplicationManager().runApplication(app); // Where app is the application descriptor of "DemoMI"
The main() method will be called. However, how can I pass the Message object around from within the "run" method to the intended application "com.demo.DemoApp"?  I was trying to use a static variable "ContactsDemoMenuItem mi", but I always got "null" as the value when the "main()" is called.

My environment:

  • JDE: 4.2.0
  • Device: Pearl 8100 with OS 4.2.0

import net.rim.device.api.system.*; import net.rim.device.api.ui.component.Dialog.*; import net.rim.blackberry.api.menuitem.*; import net.rim.blackberry.api.pdap.*; import javax.microedition.pim.*; import net.rim.blackberry.api.mail.Message; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; public final class DemoMI extends Application { private static final String ARG_LAUNCH_CONTACT_DEMO = "1"; private static ContactsDemoMenuItem mi; public static void main(String[] args) { if(args == null || args.length == 0) { DemoMI app = new DemoMI(); app.enterEventDispatcher(); }else { String appToLaunch = args[0]; if(ARG_LAUNCH_CONTACT_DEMO.equals(appToLaunch)) { new com.demo.DemoApp(mi.getMessage()).enterEventDispatcher(); } } } DemoMI() { long locationToAddMenuItem = ApplicationMenuItemRepository.MENUITEM_EMAIL_VIEW ; ApplicationMenuItemRepository amir = ApplicationMenuItemRepository.getInstance(); ApplicationDescriptor app = ApplicationDescriptor.currentApplicationDescriptor(); app = new ApplicationDescriptor(app, new String[]{ARG_LAUNCH_CONTACT_DEMO}); mi = new ContactsDemoMenuItem(app); amir.addMenuItem(locationToAddMenuItem, mi); System.exit(0); } private static class ContactsDemoMenuItem extends ApplicationMenuItem { ApplicationDescriptor app; Message msg; ContactsDemoMenuItem(ApplicationDescriptor app) { super(20); this.app = app; } public String toString() { return "My Menu Item"; } public Message getMessage() { return msg; } public Object run(Object context) { if ( context instanceof Message ) { msg = (Message)context; try { Application.getApplication.requestForeground(); //ApplicationManager.getApplicationManager().runApplication(app); }catch(Exception e) { e.printStackTrace(); } } else { throw new IllegalStateException( "Context is null, expected a Contact instance"); } return null; } } }


BlackBerry Development Advisor
Posts: 15,813
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Custom menu item to Message application: two issues

What you are seeing is the expected behaviour.  The ApplicationMenuItem is running in a different process from where your application was run.  You have a couple of options.


You could store the Message object in the RuntimeStore when the ApplicationMenuItem is invoked.  Your application that you are starting could then read the Message object from the RuntimeStore.


Or you could create an application based on the design shown here:


How To - Allow a background listener to detect and update a GUI application
Article Number: DB-00406


Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.