Welcome!

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

Prevent a UiApplication from being listed in the application switcher

by BlackBerry Development Advisor ‎02-16-2010 01:36 PM - edited ‎09-16-2010 02:30 PM (3,776 Views)

Summary

 

This article applies to the following:

  • BlackBerry® smartphones based on Java® technology

Details

 

In order to make a UiApplication not appear on the application switcher menu (for example, to make one that occasionally runs in the background), simply override the method protected boolean acceptsForeground() so that it returns false.

 

This will also prevent the application from taking the foreground. For example, if the BlackBerry smartphone user switches tasks and then clicks on the ribbon icon again, nothing will happen. In this situation, the BlackBerry smartphone user is unable to access or exit the program without restarting the BlackBerry smartphone.

 

To prevent this, use the RuntimeStore and ApplicationManager.launch() to change it back and foreground the application, as displayed by the following code:

 

 

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.system.RuntimeStore;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.ApplicationManagerException;

public class HidingUIApplicationDemo extends UiApplication {
    //"net.rim.samples.HidingUIApplcationDemo"
    private static final long ID = 0xe7fd03c2d03ab47fL;

    private MainScreen _mainScreen;

    private static HidingUIApplicationDemo _app;

    private boolean _acceptsForeground = true;

    public HidingUIApplicationDemo() {
        _mainScreen = new MainScreen();

        LabelField label = new LabelField("Select 'Hide'the menu" + from
            " to remove this from the application switcher.");
        _mainScreen.add(label);

        MenuItem hideItem = new MenuItem("Hide", 1, 1) {
            public void run() {
                setAcceptsForeground(false);
            }
        };
        _mainScreen.addMenuItem(hideItem);

        pushScreen(_mainScreen);
    }
    //We need to launch it a certain way, or if the user backs
    //out when the application is hidden, they won't be able to
    //get back in or close it without resetting the device.
    public static void main(String[] args) {
        RuntimeStore runtimeStore = RuntimeStore.getRuntimeStore();
        //If the application is run from the ribbon, it will have         //no arguments
        if (args == null || args.length == 0) {
            //Check if an instance of this app is already
            //running and saved in the store
            _app = (HidingUIApplicationDemo) runtimeStore.get(ID);
            if (_app == null || !_app.isAlive()) {
                try {
                    //If it is, we launch a new instance of our                     //application with an argument
                    ApplicationManager.getApplicationManager().launch(
                            "HidingUIApplicationDemo?show");
                } catch (ApplicationManagerException e) {
                    Dialog.alert("ERROR launching application: " + e);
                }
            //If the app is already running, unhide it and bring
            //it to the foreground.
            } else {
                _app.setAcceptsForeground(true);
                _app.requestForeground();
            }
        //If we have an argument, it's being launched from
        //within another instance of the program, so we create
        //a new instance, save it to the runtime store, and
        //start the application proper.
    } else {
        _app = new HidingUIApplicationDemo();

        runtimeStore.replace(ID, _app);
        _app.enterEventDispatcher();
    }
    //This is the method to override
    protected boolean acceptsForeground() {
        return _acceptsForeground;
    }
    //This method sets whether the application will allow itself
    //to foreground
    protected void setAcceptsForeground(boolean bool) {
        _acceptsForeground = bool;
    }}

 

Contributors
Users Online
Currently online: 33 members 1,687 guests
Please welcome our newest community members: