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: 275
Registered: ‎03-10-2009
My Device: Not Specified
Accepted Solution

when is the correct time to call pushScreen()?

[ Edited ]

Hi,

 

I made a screen that simply says "Loading..." while my app runs through its processes. the problem is the screen never got pushed until after the function where pushScreen() is returns. the call looks like this:

 

 

function func() {



//I'm hoping the loading screen would be pushed right here....

app.pushScreen(loadingScreen);



//app does its stuff



return;

//but instead the screen is pushed after func() finishes.



}

 

 

 

what am I doing wrong?

 

Thanks

 

edit: i forgot to mention that this function si being called from a menu item, and in fact the menu itself does not close until after func() is done. how do i fix this?

Message Edited by babygodzilla on 03-21-2009 04:30 PM
Message Edited by babygodzilla on 03-21-2009 04:34 PM
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: when is the correct time to call pushScreen()?

That function is running on the ui thread, which means the ui will not update until it finishes... and as a result your screen will not display untill it finishes. Push your screen and then run the other stuffs in the background. If the other stuff needs to be on the ui thread also, then push the screen and stick the rest in a UIApplication.invokeLater().
Developer
Posts: 275
Registered: ‎03-10-2009
My Device: Not Specified

Re: when is the correct time to call pushScreen()?

Hi,

 

Thanks for your reply. I'm fairly new to BB programming so I'm not familiar with the whole structure yet. How do you mean that it's on the UI thread? How do I take the code out of the UI thread then?

 

Thanks!

Developer
Posts: 136
Registered: ‎02-12-2009
My Device: Not Specified

Re: when is the correct time to call pushScreen()?

hi,

 

I think you should use a different thread that waits for a specific response, and when you get the response push your required screen.

I mean keep a thread that listens to your response, parse the response and the push the appropriate screen.

 

hope this helps

 

Regardsm

SmartFish

 

mark this as solution if you get what you need!!

_____________________________________________________________________

Thank the person who helped you by clicking Kudos! star near their post..
If you start a new thread, then resolve it using Accepted Solution button..
Leave solved threads for reviewing purposes - Start a new thread for new questions..
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: when is the correct time to call pushScreen()?

The UI Thread is also called the Event Thread.  I think you may find this Video useful.

 

What is the Event Thread?

http://www.blackberry.com/DevMediaLibrary/view.do?name=WhatistheEventThread

Developer
Posts: 275
Registered: ‎03-10-2009
My Device: Not Specified

Re: when is the correct time to call pushScreen()?

[ Edited ]

Thanks for all the replies. I think I understand better now, but I'm still not sure how to fix this.

 

I'm still not sure how to separate code and run it in a thread other than the UiThread. I think I have to make a separate Thread class to do all the actual logic?

Message Edited by babygodzilla on 03-23-2009 04:04 AM
Developer
Posts: 48
Registered: ‎02-10-2009
My Device: Not Specified

Re: when is the correct time to call pushScreen()?

[ Edited ]
new Thread(new Runnable() {
    public void run() {
        //thread instructions here.
    }
}).start();

 

That should point you in the right direction...

Message Edited by cowkingcoder on 03-23-2009 09:34 AM
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: when is the correct time to call pushScreen()?

Or, somewhat simpler:

 

new Thread() { public void run() { // do the work here } }.start();

Somewhere in run(), once the work is done, you can call Application.invokeLater(), passing a Runnable to update the UI with the results. (Alternatively, you can update the UI from a separate thread by synchronizing on the object returned by Application.getEventLock(), but that's more complicated.)

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 48
Registered: ‎02-10-2009
My Device: Not Specified

Re: when is the correct time to call pushScreen()?

Yea, Ted's method works fine as well. And my reasons for using my method are theoretical, but indulge my OCD for a second. Smiley Wink

 

Ted's method extends Thread. Thus, you are saying whatever is inside this method extends the functionality of Thread. Though theoretically, you are not adding any value to the Thread class at all. Instead, you just want to use a Thread.

 

My method implements Runnable and uses a Thread. By implementing Runnable, I'm saying that my instructions conform to the Runnable contract and is thus runnable.

 

It's a very minor and small difference I know... But it's the difference between "is a Thread" versus "uses a Thread". And I'm sure that's a big deal to all of you out there! Smiley Wink

New Developer
Posts: 20
Registered: ‎07-21-2008
My Device: Not Specified

Re: when is the correct time to call pushScreen()?

mreed's solution tells you how to get off of the Event thread in the way that you want. 

UIApplication.invokeLater(new Runnable(){ public void run() { //do stuff });

invokeLater() will execute your runnable once the event thread yields.