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: 237
Registered: ‎07-20-2010
My Device: Not Specified

how many screens can I push in a new thread?

 


public MyCity() { ... s0 = new LoadingScreen(); s0.startProgress(); pushScreen(s0); new Thread(){ public void run(){ if (serversync()){ mainmenu=new MainMenuScreen(MyCity.this); s0.stopThread(); synchronized(Application.getEventLock()){ Ui.getUiEngine().pushScreen(mainmenu); //Ui.getUiEngine().pushScreen(new LocationScreen(MyCity.this,"Establishing ...")); //Dialog.alert("hi there"); // if still getting coordinates from gps show location screen if (MyCity.gps ){//&& !eMyCity.gpsfirstlock) { //Dialog.alert("hi there 2"); // Ui.getUiEngine().pushScreen(new LocationScreen(MyCity.this,"Establishing ...")); //getActiveScreen().invalidate(); } } } else { s0.stopThread(); final Dialog dialog = new Dialog(Dialog.D_OK,"Server Synchronization Failed. You may have limited or no network connectivity. Exiting. ", Dialog.OK, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),0); UiApplication.getUiApplication().invokeAndWait(new Runnable(){ public void run(){ dialog.doModal(); } }); //Ui.getUiEngine().pushScreen(new LocationScreen(TasteMyCity.this,"Select your location.")); //getActiveScreen().invalidate(); System.exit(1); }

The above is my code, if I uncomment the second pushscreen inside the new thread I get a RunetimeException.

This code is in the main class constructor of my app so it fires immediatly.  The mainMenuScreen is the first to appear.   I want to cause a second screen to appear if the system is still trying to get the location by GPS so the users can change the get location method if they don't hant to wait for GPS to connect.  How can I cause the second screen to appear after the first?

 

 

They code used to determine if it is necessary is:

 

if (MyCity.gps && !MyCity.gpsfirstlock) {
  Ui.getUiEngine().pushScreen(new        LocationScreen(MyCity.this,"Establishing ..."));
}				        

 

 

 

 

 

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: how many screens can I push in a new thread?

Are you sure that the RuntimeException is being thrown by pushScreen and not by the call to the LocationScreen constructor?

 

Also, regarding the last two calls (currently commented out) just before System.exit(1) in the else part: these need to be run on the UI thread or while holding the event lock.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 237
Registered: ‎07-20-2010
My Device: Not Specified

Re: how many screens can I push in a new thread?

What is inside the mainmenuscreen class i add to the onexposed method the lines:

 

if (TasteMyCity.gps && !TasteMyCity.blink) {
			//Dialog.alert("hi there 2");
    		Ui.getUiEngine().pushScreen(new LocationScreen(orig,"Establishing Location via GPS/Cell..."));
}

 

should that work and will i need to add any thread stuff?

 

Developer
Posts: 237
Registered: ‎07-20-2010
My Device: Not Specified

Re: how many screens can I push in a new thread?

I tried this - commenting out the onDisplay method in the MainMenuScreen class and trying the other method onUIEngineAttached:

 

    /*protected void onDisplay(){
    	orig.popS0();
    	super.onDisplay();
    }*/
    
    protected void onUiEngineAttached (boolean att){
    	super.onUiEngineAttached(att);
    	orig.popS0();
    	if (!orig.nogps && this.orig.useSatellite() && (this.orig.qc == null || MyCity.blink)){
			//Dialog.alert("hi there 2");
    		synchronized(Application.getEventLock()){
    			Ui.getUiEngine().pushScreen(new LocationScreen(orig,"Establishing ..."));
    		}
        }
    	
    }

 but i just get frozen on  a black screen.

 

Developer
Posts: 237
Registered: ‎07-20-2010
My Device: Not Specified

Re: how many screens can I push in a new thread?

in fact when I run that in run mode i get another runetimeexception

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: how many screens can I push in a new thread?

A lot of runtime exceptions are caused by making calls that affect the user interface from outside the event thread.

 

What are you trying to accomplish, exactly? What's the user experience you're trying to achieve?




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 237
Registered: ‎07-20-2010
My Device: Not Specified

Re: how many screens can I push in a new thread?

I want to cause a second screen ( the SetLocation screen) to appear if the system is still trying to get the location by GPS so the users can change the get location method if they don't hant to wait for GPS to connect.  How can I cause the second screen to appear after the first?

I also tried it this way but just get a black screen after the splash screen loads:

 

protected void onUiEngineAttached (boolean att){
    	super.onUiEngineAttached(att);
    	orig.popS0();
    	if (!orig.nogps && this.orig.useSatellite() && (this.orig.qc == null || MyCity.blink)){
	    	UiApplication.getUiApplication().invokeAndWait(new Runnable(){
		public void run(){
		  LocationScreen locscr = new LocationScreen(orig,"Establishing Location via GPS/Cell...");
					        Ui.getUiEngine().pushScreen(locscr);
      					orig.repaint();
				}
			});
        }
    	
    }

 

 

 

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

Re: how many screens can I push in a new thread?

Being brutal here, I would suggest that you throw away all this code and move to an Observer Pattern for this processing.

 

In other words.  have a Screen that starts your background processing and acts as an observer to it.  It will get informed when that is completed.  If the user does not wish to wait, have a button or menu option in the screen, it fires a message to the background Thread that tells it that it is no longer required.

 

You Background processing should not be responsible for pushing Screens.  You should let your Screens manage the user interaction rather than having to worry about it in the Background processing. 

 

I would also try to use

UiApplication.getUiApplication().invokelater(...).

 

I would avoid using the mechanisms you are using, i.e.

Ui.getUiEngine().

and

synchronized(Application.getEventLock()){

as they should not be needed in this processing, which is not running in the Background (you have a Background Thread, which is different). 

 

I get the impression that you do not understand the Event Thread, but I think working the processing so that the Screen (User) is in control will help you understand it. 

 

But this is just my opinion. 

Developer
Posts: 237
Registered: ‎07-20-2010
My Device: Not Specified

Re: how many screens can I push in a new thread?

I already have a button  and a menu option for allowing the user to bring up the setlocation screen but I think my users my not know to use them, so having the screen appear first might be helpful.  Is there a way to choose which screen first apppears based on an if-else statement?

Currently the MainMenuScreen is set as the first to appear.

Developer
Posts: 237
Registered: ‎07-20-2010
My Device: Not Specified

Re: how many screens can I push in a new thread?

to heck with it, i'll just  add another button to the mainmenuscreen and the users will have to think of using it   assuming if they can't figure out how to use the other two options - the footer button and the menu item.