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

Reply
New Contributor
Posts: 3
Registered: ‎09-12-2012
My Device: I'm a developer
My Carrier: None
Accepted Solution

[BB 5.0] Cannot move to foreground

I know the similar problems have been discussed many times already, but looks like I'm still too much noob to solve my problem. An the problem is the following.

I'm trying to create a simple GUI application. It starts automatically, adds a special menu item to the system menu and goes to the background hidden from the applications list. Once the menu item (mentioned above) is clicked, the app should show itself to the user, do something and go back to the background. I used this article as a guide.

My code is the following (BTW, how do I format code here?):

 

public class BackTest extends UiApplication {

     private static final long APP_ID = 0xda989447901484c8L;

     private boolean isHidden = true; 

     public static void main( String[] args ) {
         RuntimeStore store = RuntimeStore.getRuntimeStore();

         BackTest theApp = null;

         if (args == null || args.length == 0) {
            theApp = (BackTest)store.get(APP_ID);
            if (theApp != null) {
                theApp.setHidden(false);
                theApp.requestForeground();
            }
        } else {
            final BackTest app = theApp = new BackTest();
            store.replace(APP_ID, theApp);

            theApp.createMenuItem();
            theApp.setHidden(true);
            theApp.invokeLater(new Runnable() {
                public void run() {
                    app.moveToBackgroundOnStart();
                }
            });
            theApp.enterEventDispatcher();
        }
    }

    public BackTest() {
        pushScreen( new BackTestScreen() );
    }

    private void moveToBackgroundOnStart() {
        ApplicationManager man = ApplicationManager.getApplicationManager();
        boolean keepGoing = true;
        while(keepGoing) {
            if (man.inStartup()) {
                try {
                    Thread.sleep(500);
                } catch (Exception ex) {}
            } else {
                this.requestBackground();
            }
        }
    }

    protected boolean acceptsForeground() {
        return !this.isHidden();
    }

    protected boolean isHidden() {
        return isHidden;
    }

    protected void setHidden(boolean isHidden) {
        this.isHidden = isHidden;
    }

    private void createMenuItem() {
        int placement = 0x350111;
        ApplicationMenuItem item = new ApplicationMenuItem(placement) {
            public String toString() {
                return "Just Do It";
            }

            public Object run(Object context) {
                try {
                    ApplicationManager.getApplicationManager().launch("BackTest");
                } catch (ApplicationManagerException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };

        ApplicationMenuItemRepository repo = ApplicationMenuItemRepository.getInstance();
        repo.addMenuItem(ApplicationMenuItemRepository.MENUITEM_SYSTEM, item);
    }
}

 

My problem is that when I click the "Just Do It" menu item, the app does not show up. I can see in the debugger, that requestForeground is called, but nothing happens after that. I've tried various things, like using GlobalEventListener instead of launching the app again (looks like background app does not receive events) or calling requestForeground directly in the menu item handler, but nothing happens.

What am I doing wrong? 

 

P.S. The BB JRE version is 5.0.

Developer
Posts: 152
Registered: ‎07-28-2011
My Device: Bold 9900
My Carrier: MTN

Re: [BB 5.0] Cannot move to foreground

Instead of calling:

 

 ApplicationManager.getApplicationManager().launch("BackTest");

 

Do the following:

 

UiApplication.getUiApplication().requestForeground();

 

New Contributor
Posts: 3
Registered: ‎09-12-2012
My Device: I'm a developer
My Carrier: None

Re: [BB 5.0] Cannot move to foreground

Thank you for looking into this.
Unfortunately, doing UiApplication.getUiApplication().requestForeground() does not help: this code seems to activate the app that is currently active, not my app sitting in the background.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: [BB 5.0] Cannot move to foreground

[ Edited ]

Interesting, code looks fine to me.  Sorry don't have time to test this myself ATM - maybe later. 

 

There is one thing I would change:

 

            theApp.invokeLater(new Runnable() {
                public void run() {
                    app.moveToBackgroundOnStart();
                }
            });

 

This in theory, will run your 'stall' processing, specifically the sleep, on the Event Thread.  You don't actually need the request background to wait because if your app is up enough to run things on its Event Thread, then it can cope with Request background.  So you could just run the requestBackground

 

If you have other things you plan to wait for at the start, then in the Runnable that you invokeLater, start a new Thread.  Sounds a bit convoluted I know.  

 

I don't think this is causing you a problem in this circumstance. 

 

I'd like you to try moving your code from the main() into the ApplicationMenu run method.  There is no problem with that code getting your application instance and then running requestForeground() on it. 

 

If this doesn't help, can you do two things:

 

1) Create activate() and deactivate() methods in your Application a,d just put something out to System.out when these are executed - call super too of course!  See Application to determine what these do. 

 

2) put a break point in acceptsForeground() and see when in the scheme of things it gets called.  I have some notion that it is actually invoked earlier than you would expect. 

New Contributor
Posts: 3
Registered: ‎09-12-2012
My Device: I'm a developer
My Carrier: None

Re: [BB 5.0] Cannot move to foreground

Wow, thanks! 

Just two simple modifications:

1. replacing app.moveToBackgroundOnStart(); with just app.requestBackground();

2. running the following in the menu item handler:

setHidden(false);
requestForeground();

and I'v got it working.

Now it seems to be a good time to do some RTFMing to find out why is it working Smiley Happy

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: [BB 5.0] Cannot move to foreground

I suspect you could also have replaced this code in main()

 

            if (theApp != null) {
                theApp.setHidden(false);
                theApp.requestForeground();
            }

 

with something like this:

 

            if (theApp != null) {
theApp.invokeLater(new Runnable() {

public void run() {

                theApp.setHidden(false);
                theApp.requestForeground();

}

});

            }

 

would also have worked.  Actually I like that slightly better than doing it in AppMenu, but only slightly.