this is how to implement “switch application” [1]

Well here is the code:

In all your Screens makeMenu method, add this:
if(instance == 0) // Full Menu
menu.add(new SwitchApplicationMenuItem());

Now create the following file, and add it to your project.


/*
* SwitchApplicationMenuItem.java
*
* Created by Lionscribe based on code from dangihitesh
* Free for anyone to use.
*/

package ;

import net.rim.device.api.system.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;


/**
*
*/
class SwitchApplicationMenuItem extends MenuItem
{
ApplicationManager _appm;
PopupScreen _pop;
LabelField _appname;
static boolean _colorSupported = Graphics.isColor();

public SwitchApplicationMenuItem()
{
super("Switch Application", 268501007, 268501007); // 268501007 comes right before the Close (0x10010010) Menu
}

private final class ApplicationIconField extends BitmapField
{
private Bitmap _icon;
private String _name;
private int _pcid;
ApplicationIconField(ApplicationDescriptor appdescriptor)
{
super(appdescriptor.getIcon(), Field.FOCUSABLE);
_icon = appdescriptor.getIcon();
if (_icon == null) // Just for safety
_icon = Bitmap.getPredefinedBitmap(Bitmap.QUESTION);
_name = appdescriptor.getName();
if (_name.length() >= 2 && _name.charAt(1) == 0x0332) // remove underscores
{
StringBuffer temp = new StringBuffer(_name);
temp.deleteCharAt(1);
_name = temp.toString();
}
_pcid = _appm.getProcessId(appdescriptor);
setSpace(10, 3);
}

protected void paint(Graphics graphics)
{
int x, y, width, height, iconWidth, iconHeight, xOffset, yOffset;
width = getContentWidth();
height = getContentHeight();
iconWidth = _icon.getWidth();
iconHeight = _icon.getHeight();
if (iconWidth <= width)
{
x = (width - iconWidth) / 2;
xOffset = 0;
}
else
{
x = 0;
xOffset = (iconWidth - width) / 2;
iconWidth = width;
}

if (iconHeight <= height)
{
y = (height - iconHeight) / 2;
yOffset = 0;
}
else
{
y = 0;
yOffset = (iconHeight - height) / 2;
iconHeight = height;
}
if (_colorSupported && this.isFocus())
{
int circleSize = Math.min(iconHeight, iconWidth);
int maxSize = Math.min(height, width);
if (circleSize < maxSize)
circleSize = Math.min(maxSize, circleSize + ((maxSize - circleSize) / 2));
else
circleSize = maxSize;
graphics.setColor(Color.YELLOW);
graphics.fillRoundRect((width - circleSize) / 2, (height - circleSize) / 2, circleSize, circleSize, circleSize, circleSize); // circle
}
if (_colorSupported && _icon.getBitsPerPixel() == 1) // add some color to Mono images
{
graphics.setColor(Color.DARKBLUE);
graphics.rop(Graphics.ROP_SRCMONOEXPAND_ALPHA, x, y, iconWidth, iconHeight, _icon, xOffset, yOffset);
}
else
{
graphics.drawBitmap(x, y, iconWidth, iconHeight, _icon, xOffset, yOffset);
}
}

protected void drawFocus(Graphics graphics, boolean on)
{
if (!_colorSupported)
super.drawFocus(graphics, on);
// else - Do nothing. We handle it already in Paint
}

protected boolean trackwheelClick(int status, int time)
{
HandleSelect();
return true;
}

protected boolean keyChar(char key, int status, int time)
{
if (key == '\n')
{
HandleSelect();
return true;
}
return super.keyChar(key, status, time);
}

public void HandleSelect()
{
_pop.close();
_appm.requestForeground(_pcid);
}
protected void onFocus(int direction)
{
super.onFocus(direction);
_appname.setText(_name);
}
public int getPreferredHeight()
{
return 48;
}
public int getPreferredWidth()
{
return 48;
}
}

public void run()
{
try
{
_appm = ApplicationManager.getApplicationManager();
ApplicationDescriptor [] appdes = _appm.getVisibleApplications();
int tot_app = appdes.length;
VerticalFieldManager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL);
ApplicationIconField focusField = null;
for (int i = 0; i < tot_app; i++)
{
ApplicationIconField iconField = new ApplicationIconField(appdes[i]);
hfm.add(iconField);
if (i <= 1) // We want the second one, if it exists
focusField = iconField;
}
vfm.add(hfm);
_appname = new LabelField(" ",Field.NON_FOCUSABLE);
vfm.add(_appname);

_pop = new PopupScreen(vfm, PopupScreen.DEFAULT_CLOSE);
UiApplication.getUiApplication().pushScreen(_pop);
focusField.setFocus();

}
catch (Exception e)
{
System.out.println("Error from Switch Application:" + e);
}

}

}

[1] http://www.blackberryforums.com/developer-forum/114028-how-implement-switch-application-menu-item.html