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
Contributor
Posts: 13
Registered: ‎10-08-2009
My Device: Not Specified

Question Using pushModalScreen() method

[ Edited ]

I have a PopUpScreen that I'm trying to have pop up in front of my main Screen class.  I've been able to make the PopUpScreen pop up when needed, however it seems the main Screen class does not stop processing.  I would like to pause (for lack of a better word) what is happening on the main Screen until the PopupScreen is closed (using close() ).

 

Here's an example of what I am trying to do...

 

UiApplication.getUiApplication().invokeAndWait(new Runnable() {

  public void run() {

  UiApplication.getUiApplication().pushModalScreen(new AddPopupScreen(MainProjectScreen.this));

  }

 });

 

I have tried using UiApplication.getUiApplication().invokeAndWait and invokeLater methods.  InvokeAndWait causes a RunTimeException on the pushModalScreen call, and invokeLater invokes the PopUpScreen, but allows the main Screen class to continue processing before the PopupScreen returns.

 

Also, do I need to use invokeAndWait() when I am trying to use the pushModalScreen call?  I'm a little confused, but I thought pushModalScreen automatically paused what was going on with the calling class until the PopupScreen (or whatever screen is set by pushModalScreen) is closed.

Any ideas on what I am doing wrong? 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Question Using pushModalScreen() method

UiApplication.pushModal is correct.

 

Don't understand the ncecessity for invokeAndWait(). Can you explain? You should be able to push this screen directly without invokeAndWait().

 

Contributor
Posts: 13
Registered: ‎10-08-2009
My Device: Not Specified

Re: Question Using pushModalScreen() method

I thought so too.  When I try to invoke this operation...

 

if (persistentObject.getContents() == null) {

  UiApplication.getUiApplication().pushModalScreen(new AddPopupScreen(this));

}

 

I get a runtime exception stating that pushModalScreen was called on a non-event thread.  I don't have any other threads explicitly defined in my program, so unless there's a non-event background thread trying to process this pushModalScreen call, I'm not sure why it's throwing that. 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Question Using pushModalScreen() method

[ Edited ]

What is the message that is thrown with the exception you get from invokeAndWait?? From where in your main Screen are you trying to push the modal screen? If it is from onDisplay() or similar methods, that may be a cause for weird issues.

 

You can also try a synchronized(UiApplication.getEventLock()) block. Although I have noticed on some background Threads the sync block does not work with a modal screen, yet invokeAndWait does.

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Question Using pushModalScreen() method

If you are getting this "non-event thread', then perhaps you calling from the thread context of a system listener?

 

Can you show some more code?

 

 

Contributor
Posts: 13
Registered: ‎10-08-2009
My Device: Not Specified

Re: Question Using pushModalScreen() method

It's basically just giving the Runtime Exception: pushModal called from non-event thread error within the simulator.

 

So here's a snippet of my code...

 

public class MainAppScreen extends MainScreen {

public MainAppScreen() {

buildTitle();

buildMenu();

init();

 }

 

 public void init() {

persistentObject = PersistentStore.getPersistentObject(key);

if (persistentObject == null) {

UiApplication.getUiApplication().pushModalScreen(new PopUpScreen(this));

} else {

hashTable = (Hashtable)persistentObject.getContents();

}

}

}

 

I am passing "this" into the Customized Screen object because I want to refer to one of the MainAppScreen's methods from the popup screen, after OK is clicked.  The PopupScreen is basically (the PopUpScreen class name is different from the actual PopupScreen class I am extending)...

 

public PopUpScreen extends PopupScreen implements FieldChangeListener {

private EditField name;

 

public PopUpScreen() {

super (new VerticalFieldManager());

name = new EditField("Name:", "");

add(name);

}

}

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

Re: Question Using pushModalScreen() method

I presume this is happening at startup?  You create an instance of your application, that creates an instance of your MainAppScreen, which tries to display the screen, all before the Application has indeed 'enterthedispatcher?

 

In this situation, your invokeLater code is the correct way to handle this. 

 

You can't stop the initialization of your MainScreen in this way if it is the first screen in your application, because it is stopping your application 'entering the dispatcher'.  If you do need the answer to the Popup before you complete the MainScreen, then I think the easiest option is to put the processing into the 'close' of the Popup Screen. 

 

There are other options which are probably more elegant if this grates with you. 

Contributor
Posts: 13
Registered: ‎10-08-2009
My Device: Not Specified

Re: Question Using pushModalScreen() method

You know what, that makes perfect sense, and that's exactly how my application is structured (instantiating the MainAppScreen class before enterEventDispatcher).  So it sounds like...

 

- invokeAndWait -- this is more or less trying to put the pushModalScreen operation on the event thread which isn't even started yet, which is causing the runtime error

 

- invokeLater -- allows the program to wait until it can actually put something on the event thread, but that wouldn't be until after my MainAppScreen does all of its initialization.

 

Thanks a lot for that explanation.  Why can't I simply call UiApplication.getUiApplication.pushModalScreen (or just pushScreen) in the MainAppScreen initialization (without queuing it up for the event thread).  This MainAppScreen class has several other UI components that work fine before the event dispatcher thread is invoked.

 

Thanks again!!

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

Re: Question Using pushModalScreen() method

"Why can't I simply call UiApplication.getUiApplication.pushModalScreen"

because a Modal screen implies waiting.  Nothing will get actioned, in terms of events like Key press, until you have 'entered the Dispatcher'.  So even if your screen was displayed, there is no Thread in place to process key strokes that would respond to the screen. 

 

"This MainAppScreen class has several other UI components that work fine"

Bet none of these involving waiting on an Event.