views:

386

answers:

1

I'd like to know the proper place to put GUI based sequential start-up code in my Blackberry app.

In main(), I create MyApp and enterEventDispatcher()
I have UiApplication (MyApp)
In the MyApp CTOR:
- I create a MainScreen (MyMain)
- I call pushScreen() on MyMain

When the event dispatcher starts, is there an event I can listen for in my MainScreen that will give me the event thread where I can happily do synchronous start-up tasks?

I can use invokeLater() but I want each call to block because their order is important in this phase. invokeAndWait() throws an exception in most cases where I've attempted to use it.

I've attempted the code below but I get an exception when trying to run on the "Testing 1 2 3" line.

    public class MyApp extends UiApplication {

        static public void main(String[] args) {
            new MyApp().enterEventDispatcher();
        }

        public MyApp()
        {
            MyView theView = new MyView();
            theView.startUpdateTimer();
            pushScreen(theView);

            Dialog.alert("Testing 1 2 3");
        }
    }
+1  A: 

If it's pretty quick UI stuff (creating a screen, pushing it onto the stack), do it from the main thread before calling enterEventDispatcher. You can actually do as much as you want, just the user experience will be worse if your app takes a long time.

The thread that calls enterEventDispatcher basically becomes the event dispatch thread, so you're safe to do any GUI stuff on that thread before calling enterEventDispatcher.

Specifically, don't call invokeAndWait from the main thread - that'll cause deadlock and probably an exception.

Anthony Rizk
Will the UI *work* prior to calling enterEventDispatcher? i.e. Can I do interactive UI things?e.g., To clarify, let's say I want to ask the user if they want feature X enabled. Can I say Dialog.ask() in main, get the response, make adjustments to my settings, all prior to calling enterEventDispatcher? Otherwise, I guess I'm just queuing things up to be run once the dispatcher starts.
JR Lawhorne
I tried a Dialog.alert() in main. As expected, I get an IllegalStateException. Argh. I suppose I could wrap all my startup code in an invokeLater but I'd like to know if that's the RIM way to do it.
JR Lawhorne