views:

1809

answers:

2

Hi all..

my application contains lots of images..so it takes some time to load the application..i want to show a loading screen whhile the application is being loaded...how is it possible..??

+5  A: 

Here's an example app that skeletons what your looking to do. Basically, the initial screen you push is a loading screen. During the initial startup sequence you need to spin up a new thread, do your loading stuff and then use invokeLater to push 1) make sure your in the event dispatcher and 2) to push a new screen, or in the case of this example a dialog, to the screen to have the user progress away from the loading screen.

Here's the code:

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;

/**
 * Just a test app.
 */
public class TestAppMain extends UiApplication 
{
    /**
     * Default Constructor.
     */
    private TestAppMain() {                
        pushScreen(new AppScreen(this));        
    }

    /**
     * App entry point.
     * @param args Arguments.
     */
    public static void main(String[] args) {
        TestAppMain app = new TestAppMain();                       
        app.enterEventDispatcher();
    }

    /**
     * Main application screen.
     */
    private static class AppScreen extends MainScreen 
    {
        UiApplication _app;

        /**
         * Default constructor.
         */
        public AppScreen(UiApplication app) {
            // Note: This screen just says "Loading...", but you could
            // add a loading animation.
            _app = app;
            LabelField title = new LabelField("App Name",
                    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
            setTitle(title);

            LabelField loading = new LabelField("Loading...",
                    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);

            add(loading);

            // Queue up the loading process.
            startLoading();
        }               

        /**
         * Create the loading thread. Make sure to invoke later as you will
         * need to push a screen or show a dialog after the loading is complete, eventhough
         * you are creating the thread before the app is in the event dispatcher.
         */
        public void startLoading() { 
            Thread loadThread = new Thread() {
                public void run() {
                    // Make sure to invokeLater to avoid problems with the event thread.
                    try{ 
                        // Simulate loading time
                        Thread.sleep(5000);
                    } catch(java.lang.InterruptedException e){}

                    // TODO - Add loading logic here.

                    _app.invokeLater( new Runnable() {                
                        public void run() {                    
                            // This represents the next step after loading. This just shows 
                            // a dialog, but you could push the applications main menu screen.
                            Dialog.alert("Load Complete");
                        }
                    });
                }
            };
            loadThread.start();
        }
    }
}
Fostah
Are you sure it is correct to execute long-running task in UIApplication.invokeLater method. invokeLater executes code in the provided run method right on event dispatcher thread!!!
nixau
@nixau That's a valid point. I've reworked the code to spin up a separate thread and only use invokeLater for the GUI change. Thanks
Fostah
How do I modify the code if I need to add another screen after the splash instead of the dialog alert? Thanks
Bohemian
+1  A: 

naviina.eu: Startup SplashScreen for BlackBerry
SO - Blackberry - Loading screen with animation
naviina.eu: A Web2.0/Ajax-style loading popup in a native BlackBerry application

Max Gontar
How do I push a new screen after the splash timeout instead of a dialog alert? I mean take user to the home screen after the splash screen.
Bohemian
You could write there something like getUiEngine().pushScreen(new MainScreen());
Max Gontar
But dont forget to close SplashScreen after that
Max Gontar