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
Developer
Posts: 41
Registered: ‎07-07-2009
My Device: Not Specified
Accepted Solution

Startup requestBackground failed

Hi, I have the following project with "Auto run on startup" checked, tested in OS 4.5 simulator. This project will start automatically with GUI screen. Although I have request it to go background right after pushScreen, but the GUI screen still shown without going to background. Did I miss any important commands here, or something wrong with my coding? Thanks!

 

public class Test02_KeyListener extends UiApplication implements SystemListener {


public static void main(String[] args) {

  Test02_KeyListener theApp = new Test02_KeyListener();

if (ApplicationManager.getApplicationManager().inStartup()) {        

  theApp.addSystemListener(theApp);        

} else {        

  theApp.doStartupWorkLater();        

}

  theApp.enterEventDispatcher();

  }


public Test02_KeyListener()     {     

  pushScreen(new HelloWorldScreen()); 

  UiApplication.getUiApplication().requestBackground();    

}


private void doStartupWorkLater() {        

invokeLater(new Runnable() {                

public void run() {                                     

doStartupWork();                

}        

});    

}          

 

private void doStartupWork() {         

}

 

 

public void powerUp() {

removeSystemListener(this);

}

 

 

}

 

 

final class HelloWorldScreen extends MainScreen 

  public HelloWorldScreen() 

    { 

            super(); 

            LabelField title = new LabelField("HelloWorld Sample", 

                 LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH); 

            setTitle(title); 

            add(new EditField("Testing...", "", 100, BasicEditField.FILTER_LOWERCASE | BasicEditField.FILTER_INTEGER | Field.FIELD_RIGHT)); 

    }

}
regards,
Season

 

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

Re: Startup requestBackground failed

I suspect your problem here is the order in which things are done.

 

You are doing a

UiApplication.getUiApplication()...

in the constructor of your UiApplication, so your UiApplication does not actually exist at the point.  You code is being run by the start-up Thread that I believe will become your Event Thread but your Application does not exist and so can't go into the Background.

 

So move the request background to the

doStartupWork

method, in fact you should be able to move your pushScreen there too. 

 

BTW, I would have thought that

powerUp

would have called

doStartupWork

 

One final thing.  I f you are testing the keyListener in the hope that you will be able to detect key presses from the background, I think you can stop your test now.  Search the forum for people trying to do similar things. 

Developer
Posts: 41
Registered: ‎07-07-2009
My Device: Not Specified

Re: Startup requestBackground failed

Thanks for your reply, Peter! I tried to move the pushScreen and requestBackground() to doStartupWork() as you suggested, but still did not go to background after reset. This time I tested on 4.5 and 4.6 simulator. Any other clues? Thanks!

 

public class Test02_KeyListener extends UiApplication implements SystemListener {


public static void main(String[] args) { 

  Test02_KeyListener theApp = new Test02_KeyListener();

if (ApplicationManager.getApplicationManager().inStartup()) {        

  theApp.addSystemListener(theApp);        

} else {         

  theApp.doStartupWorkLater();        

} 

  theApp.enterEventDispatcher(); 

  }


public Test02_KeyListener()     {      

}


private void doStartupWorkLater() {        

invokeLater(new Runnable() {                

public void run() {                                     

doStartupWork();                

}        

});    

}          

 

private void doStartupWork() {         

     pushScreen(new HelloWorldScreen()); 

 UiApplication.getUiApplication().requestBackground();

}

 

 

public void powerUp() {

removeSystemListener(this);

 

doStartupWork();

 

}

 

 

}

 

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: Startup requestBackground failed

If you post your code using the code option, it will be more readable but, I do not see where you are calling do startup work. On startup you register your app, then you enter the event dispatcher, but your do startup work later is only called the second time the app runs because it will almost always be called during startup the first time.

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

Re: Startup requestBackground failed

No.  I can't see what is wrong with what you are doing. 

 

Can you do the following:

a) Change UiApplication.getUiApplication().requestBackground (); to just requestBackground();

b) Put a break point in that line and make sure it is executed. 

Highlighted
Developer
Posts: 41
Registered: ‎07-07-2009
My Device: Not Specified

Re: Startup requestBackground failed

 

public class Test02_KeyListener extends UiApplication implements SystemListener {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test02_KeyListener theApp = new Test02_KeyListener();
        if (ApplicationManager.getApplicationManager().inStartup()) {
        	theApp.addSystemListener(theApp);
        } else {
        	theApp.doStartupWorkLater();
        }
		theApp.enterEventDispatcher();
	}

    public Test02_KeyListener() 
    { 
    	//display a new screen 
    	pushScreen(new HelloWorldScreen()); 
    }

    private void doStartupWorkLater() {
        invokeLater(new Runnable() {
                public void run() {
                        doStartupWork();
                }
        });
    }  
    
    private void doStartupWork() {

    }

	public void powerUp() {
		this.removeSystemListener(this);
		
		UiApplication.getUiApplication().requestBackground();
        doStartupWork();
	}

}

 

CMY, thanks for letting me know about the code option, I'm new to this forum and yes I found my coding attached previously hardly read too.

 

Peter, thanks for your help! I have tested with requestBackground() instead of UiApplication.getUiApplication().requestBackground (), but no difference. Breakpoint testing abit weird, which the app goes background with breakpoint, but stay foreground without breakpoint in my simulator. I suspected it could be the simulator issue, so it is working fine after I loaded the app to my 8320 (OS 4.5) device.

 

And I have attached a few changes here on my coding to make it perfect, so that other developers can have a clear idea on startup GUI issue. I put the pushScreen on constructor, and requestBackground on powerUp(). This is because I will load the screen at beginning, and requestBackground only on the startup. Previous coding (that pushScreen and requestBackground in doStartupWork()) worked well during startup, but it is weird if the app quit and re-launch manual, it will go background immediately.

 

 

regards,

Season