11-25-2012 02:19 PM
I've been trying to avoid the IllegalStateExceptions.. as a n00b I've been getting them quite often. I realized trying to do something to a screen the user popped (bad user, bad!) often gives an Illegal State. I tried writing a method to detect if the screen is the top most on the stack so I check that first before doing anything to the screen, so If I do anything in a thread and have to come back later I check if the screen is active first.
public boolean isActive() {
return UiApplication.getUiApplication().getActiveScreen() == this;
}
I realized a few issues/questions with this approach.
protected void invokeWhenActive(Runnable r){
// store the runnable
}
public void onScreenUiEngineAttached(Screen screen, boolean attached) {
if(attached){
// check if we have pending runnables...
UiApplication.getUiApplication().invokeLater(runn
}
}
What best practices can I keep in mind for updating screens? To give a concrete example. Users select a category of points of interests (e.g. police station, hospital) from one screen then I push the result listing screen which starts by displaying a loading animation. Then it updates when I get the info.
Solved! Go to Solution.
11-25-2012 06:15 PM
IllegalStateException can actually happen for a variety of reasons. But that said, I am not aware of any that are a result of a screen not being the active screen. So I suspect your presumption that this is the cause is not correct, I think there is something else that goes one as well as the screen being popped that is creating your problem.
I generally print out the exception (<exception>.toString()) to find out exactly why I have got it. As examples of the sort of thing you will find
a) you can get this by setting focus on a field that is not actually on the screen - I think the message will say something about "not parented" or "not on Screen" (note that you can set Focus on a Field for a screen that is NOT on the top of the stack)
b) adding a Field to a Manager when the Field is already added to a Manager, I think the message will say "already parented".
So unless you print out the detail message, you won't be able to resolve your problem.
With respect to you issue about updating screen, remember that when you are updating a screen, you must have the Event Lock - this is simply a lock that makes sure only one thing is updating the UI at once. Now all the event handlers (key, menu etc.) run on the Event Thread, which means they all have the lock. You can take advantage of this too. You can schedule a Runnable t be run on the Event Thread, when it will have the Event lock. You do this using this 'incantation':
UiApplication.getUiApplication(),invokeLater(new Runnable() {
public void run() {
// your UI updating code
}
});
So you should use this sort of process when you get the information back that you want. So push the 'loading screen, start your Download Thread, have the Download Thread use this incantation to pop the loading screen and update the UI.
Hope this answers the questions:
See more here:
http://supportforums.blackberry.com/t5/Java-Develo
12-02-2012 06:22 PM