views:

336

answers:

3

I've recently run into a snag while putting on the finishing touches for my BlackBerry app. I'm building a login screen which, if the user is successful in logging in, goes to a data loading screen, and then to a home screen. From the home screen, you can use the app. Everything works great but one thing, I can't seamlessly move from the login screen to the loading screen, to the home screen. I can move from the login screen to the loading screen ok, because I'm doing that via a button click which is on the GUI thread, but then I have the login screen at the bottom of the stack and can't get it out using the dismiss method. Once in the loading screen, I can't push the home screen because I'm not doing it via the gui method, though I'm able to update the GUI via the following piece of code:

   private void checkData(){

            Timer loadingTimer = new Timer();
    TimerTask loadingTask = new TimerTask()
    {

        public void run()
        {
            // set the progress bar
            progressGaugeField.setValue(DataManager.getDataLoaded());

            // for repainting the screen
            invalidate();
        }
    };
    loadingTimer.scheduleAtFixedRate(loadingTask, 500, 500);
}

Does anyone know how to solve my problem of moving seamlessly from the login screen to the loading screen to the home screen? Note: once I'm at the home screen I'd like to have it be the only screen on the stack.

Thanks!

A: 

If you know in code when you need to push the screen, you can use the UiApplication pushScreen function. You start by getting a handle to the current application that has the UI:

UiApplication currentapp = UiApplication.getUiApplication();

You can compare the active screen in order to verify it's your application:

currentapp.getActiveScreen();

Then you can use the UiApplication instance to push your new screen:

currentapp.pushScreen(homeScreen);

I hope that helps :)

Tamar
I've been doing that, except the getActiveScreen(). It causes an exception on the paint event for the new screen which won't print out :(. Any other ideas?
DanG
So you get an exception when you try to push your home screen? Which exception?
Tamar
I think putting the home screen on first makes the most sense. I can populate it when appropriate.
DanG
Sounds like a good plan :) Good luck
Tamar
+1  A: 

Have you tried putting the main screen on the stack first (from the Application) and, if no login has been attempted yet, push the login screen? In turn the login screen can push the loading screen. Once loading is completed, pop it out of the stack (from login) and close() login. You will then be back on the main screen with a logged in user.

For the loading to work and track its progress, you can use the following code

LoadingScreen lScreen = new LoadingScreen();
getUiEngine().pushScreen(lScreen);
_invokeIDGame = getApplication().invokeLater(new Runnable() {
    public void run() {
        // Check to see if the loading is done.
        // implement an isLoading function to determine it
        if (lScreen.isLoading() == false) {
        // Cancel invoking this piece of code again (normally is invoked
        // every 500 ms, as specified below)
        getApplication().cancelInvokeLater(_invokeIDGame);
        // Pop the loading screen off the stack, which returns the user to this screen
        getUiEngine().popScreen(lScreen);
         // close this screen
         close();
        }
    }
}, 500, true); // rerun this code every 500ms
ADB
Your answer is correct as well, though I can only chose one. Sorry.
DanG
+1  A: 

In this case, you may want to push the home screen of your application first and then push the login screen after it. I am not sure of your situation, but here are the assumptions I am making:

  • you are storing login information
  • if login information is not stored, you push the login screen
  • in a success case, you store login information and proceed to show the home screen
  • if login fails or is canceled, you don't display the home screen

If these assumptions are reasonably valid, the below code should work nicely for you.

public class YourApp extends UiApplication {
public YourApp() {
    final AppHomeScreen screen = new AppHomeScreen();
    pushScreen(screen);

    if(getLoginInfo() == null) {
        final LoginScreen loginScreen = new LoginScreen();
        this.invokeLater(new Runnable() {
            public void run() {
                pushModalScreen(loginScreen);
                if(getLoginInfo()==null)
                    popScreen(screen); //exit the app
                }
                //perhaps the loading screen populates info in your home screen then pops itself
                pushScreen(new LoadingScreen(screen));
            }
        );
    }
}
Eric
This works. Thanks!
DanG