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
Developer
Posts: 113
Registered: ‎09-30-2009
My Device: Bold 9700

Splashscreen / threading correct sequence/syntax?

[ Edited ]

Hi

 

My program takes about 6 seconds to load (db connection, etc.) so I decided I needed a splash screen.  I have been trying the examples in the forum, but I am not sure how to call the screen. I just need the simplest of splash screens w/ a logo and a label.

 

It seems to work, but I don't think that the program actually exits/dies completely (I say this b/c when I debug on the device, it deploys to the device the first time fine, then after running the program to test, and calling System.exit(0) to close it, the next time you debug on the device it fails to deploy and crashes the device which only happens when your program is still running in my experience)

 

My main/calling program:

 

public class myProgram extends UiApplication {

    public static void main( String[] args )
    {
    	myProgram instance = new myProgram();
        instance.enterEventDispatcher();
    }
    
    public myProgram() { 
    	pushScreen( new SplashScreen() );
    }
}

 

 

And my splash screen:

 

public class SplashScreen extends MainScreen {

    public SplashScreen() {
        VerticalFieldManager fMBanner = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
	Bitmap logoBitmap = Bitmap.getBitmapResource("image.png");
	BitmapField logoKV = new BitmapField(logoBitmap, Field.FIELD_HCENTER | Field.FIELD_VCENTER);
	fMBanner.add(logoKV);
	LabelField lbl = new LabelField("Loading...");
	fMBanner.add(lbl);
	add(fMBanner);

        DismissThread dThread = new DismissThread();
        UiApplication.getUiApplication().invokeLater (dThread);
    }
    public void dismiss() {
    	UiApplication.getUiApplication().pushScreen(new LogonScreen());
    }
    private class DismissThread implements Runnable {
        public void run() {
        	dismiss();
        }   
    }     
	
} //class SplashScreen

 

 

And this calls my logon screen which calls onClose() which is:

 

public boolean onClose() {
    	//close db connection here
    	System.exit(0);
    	return true;
} //onClose

 

 

Is this correct?  I think that maybe calling the logon screen in a thread causes the other screens to stay alive when the program exists?  Or do I have somthing else going on?

 

Running on a BB Bold 9700 using v5.0.0.

 

Regards,

Scott

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Splashscreen / threading correct sequence/syntax?

What do you mean by "crashes the device"?  Does it cause the device to reboot?  Does the keyboard become unresponsive?  I don't see you doing anything on a Thread other than the event (UI) thread.  I assume you have some other code there as well - check where it's processed.

 

Another question: how do you close your LogonScreen?  Do you just popScreen() it?  Or do you call its close() or onClose() from some other piece of code?

 

Also, why don't you popScreen() your SplashScreen from your dismiss() method?

 

As a generic advice:

One of the great examples of such splash screens is this knowledge base article (there are also part 2 and part 3):

 

Sample "Please Wait" Screen - part 1

 

Take a look there, compare with what you are doing in your code and see if there are any glaring differences.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 113
Registered: ‎09-30-2009
My Device: Bold 9700

Re: Splashscreen / threading correct sequence/syntax?

 


arkadyz wrote:

What do you mean by "crashes the device"?  Does it cause the device to reboot?  Does the keyboard become unresponsive?  I don't see you doing anything on a Thread other than the event (UI) thread.  I assume you have some other code there as well - check where it's processed.


Yes, it reboots.  Only using the UI thread.

 


arkadyz wrote:

Another question: how do you close your LogonScreen?  Do you just popScreen() it?  Or do you call its close() or onClose() from some other piece of code?


I override onClose, adding System.exit(0).  It is called by a button press (user hits the exit button).

 


arkadyz wrote:
Also, why don't you popScreen() your SplashScreen from your dismiss() method?

I have tried it with and without.  Was just trying stuff to get it to work.  At one point dismiss() was also this:

 

public void dismiss() {
	UiApplication.getUiApplication().popScreen(this);
    	UiApplication.getUiApplication().pushScreen(new LogonScreen());
}

 

 

 

 

 

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

Re: Splashscreen / threading correct sequence/syntax?

[ Edited ]

Here is the official KB about creating splash screens:

 

http://supportforums.blackberry.com/t5/Java-Development/Create-a-splash-screen/ta-p/444959

 

There do seem to be some issues with your code.

 

DismissThread dThread = new DismissThread();
UiApplication.getUiApplication().invokeLater (dThread);

 

This is not running a separate Thread.  This will execute whatever is in your run method of DismissThread in the Event Thread. 

 

So let us have a look there:

dismiss();

 

So all this is going to do is immediately call dismiss.  I suspect you wanted your SpalshScreen to display for a moment or two, but you don't tell it that.

 

If we now look at dismiss:

UiApplication.getUiApplication().pushScreen(new LogonScreen());

 

You are pushing a new Screen.  So you now have two screens on the display stack, the Splash Screen and your Login Screen.  I think you only need one, so you should pop the SplashScreen.

 

Regarding your other questions, System.exit kills your application.  Regardless of how many screens you might have outstanding.

 

I see that things have changed a little since I started this post.  I hope this is still helpful. 

Developer
Posts: 113
Registered: ‎09-30-2009
My Device: Bold 9700

Re: Splashscreen / threading correct sequence/syntax?

Peter

 

The 'splash' part is working fine.  Since it takes about 5 or 6 sec. for my program to load, the splash screen is there for that amount of time.  The issue I am having is the device rebooting every other time I deploy to device.

 

So if System.exit kills everything?  Then I guess there is another issue here.  I will try and remove the splash screen logic and see if that helps.

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

Re: Splashscreen / threading correct sequence/syntax?

If this splash screen is staying there for 5 seconds, then I strongly suspect that you are running something in the Event Thread.  The way I read the code I see, it will instantly dismiss the SplashScreen, unless there was something blocking it.  I presume you are testing this on the Simulator?  The Simulator will let you get away with this, blocking code will not work on the device.

 

I would recommend that you push the Splash Screen, and start a background Thread that does your processing.  Have the background Thread call dismiss on your splash screen when it has finished. 

Developer
Posts: 113
Registered: ‎09-30-2009
My Device: Bold 9700

Re: Splashscreen / threading correct sequence/syntax?

Peter

 

I thought I was using that KB.  I was just taking out all the stuff I didn't need like a timer (b/c it takes so long for my program to load anyway) and capturing key/screen events (which I also don't need).

 

-Scott

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

Re: Splashscreen / threading correct sequence/syntax?

The timer delays the request to dismiss, that is true.

 

So is there code we are not seeing here, for example stuff that runs between these lines?

 

add(fMBanner);

DismissThread dThread = new DismissThread();

 

 

Developer
Posts: 113
Registered: ‎09-30-2009
My Device: Bold 9700

Re: Splashscreen / threading correct sequence/syntax?

 


peter_strange wrote:

The timer delays the request to dismiss, that is true.

 

So is there code we are not seeing here, for example stuff that runs between these lines?

 

add(fMBanner);

DismissThread dThread = new DismissThread();

 

 


Nope.  I assumed it was staying there b/c of the time it took the second screen to load.  And no, I am not running on the simulator.

 

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

Re: Splashscreen / threading correct sequence/syntax?

I don't see any 'load' processing - where is that?