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: 26
Registered: ‎06-10-2011
My Device: Blackberry 9800 Simulator
My Carrier: Fledge Simulated Network
Accepted Solution

MainScreen onUnfocus()

Hello,

 

I am trying to have a bitmapfield updated whenever the screen is not on the top of the display stack so that when users returns to that screen, a different bitmap is shown.

 

i've tried various ways including onObscure and onExposed, however those will also update the bitmapfield whenever the screen shows a status or whenever the menu item is brought up.

 

My last bet is the onUnfocus event, however it doesnt seem to be trigerred. My code are as follows.

 

	protected void onUnFocus() {
		super.onUnfocus();
		pageAd.next();
	}

 My pageAd looks like this.

public class PageAd extends BitmapField {
	int totalAds=5;
	int currentAd=1;
	String fileSuffix=".JPG";
	String filePrefix="ad1_";
	
	
	public PageAd()
	{
		updateBitmap();
	}
	
	public void next()
	{
		if (currentAd != totalAds){
			currentAd+=1;
		} else {
			currentAd=1;
		}
		updateBitmap();
	}
	
	private void updateBitmap()
	{
		String fileName = filePrefix + String.valueOf(currentAd) + fileSuffix;
		Bitmap bitmap = Bitmap.getBitmapResource(fileName);
		setBitmap(bitmap);
	}

}

 Any help is greatly appreciated. Thanks in advance Smiley Happy

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: MainScreen onUnfocus()

I don't know of a better way of doing this than starting a thread to monitor when the active screen changes in the Ui. When the thread detects a screen change, you can make a call to next() to cycle the images.

 

Scott

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

Re: MainScreen onUnfocus()

You are right that onObscured and onExposed are called too frequently for this to be useful.  Similarly I suspect having a Thread that attempts to check the active screen will fail, because a Menu is a popup screen. 

 

There is, as far as I am aware, no standard way to detect that your screen is no longer on the top of the display stack.  But in fact for it not to be, your screen must have pushed another screen on top of it.  So I suspect you can add a check to your own code that will do this. 

 

The one thing this will not detect is when the user swaps to another application.  In that circumstance, you will find your applications activate and deactivate get called, perhaps you can put some sort of hook in there that initiates the bitmap change. 

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: MainScreen onUnfocus()

I suppose if you use the thread I mentioned but didn't want the image to update when the menu came up, you could test whether the active screen is a PopupScreen with 'instanceof'.

 

Scott

Contributor
Posts: 26
Registered: ‎06-10-2011
My Device: Blackberry 9800 Simulator
My Carrier: Fledge Simulated Network

Re: MainScreen onUnfocus()

thanks peter & superdirt.

I was hoping to get the onUnfocus to work, as this seems to be the most straight forward way. Why isnt it running? The API it says that the onUnfocus should be "Invoked when focus is removed from this screen."

superdirt- Correct me if i'm wrong, do you mean on the MainArgs of my app, i add an onscreenchange listener, to call the PageAd.next method? I cant visualize how to do this.. can u provide more info pls? Some codes will help.. thanks Smiley Happy

peter - i dont need the bitmap to change if user swaps application. The bitmap only needs to be changed when user returns to that screen from another screen within the application. Can you elaborate more about the check you mentioned? I'm guessing placing the PageAd.next() after all PushScreen() methods.

Developer
Posts: 202
Registered: ‎04-19-2011
My Device: Blackberry Bold 9780
My Carrier: TAM

Re: MainScreen onUnfocus()

Hm... this is just my suggest

try to make your screens became static and using a thread...

So, every time you change screen from this "bitmap screen", run a thread that will load the bitmap and invalidate this "bitmap screen"..

 

correct me if i'm wrong

Highlighted
Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: MainScreen onUnfocus()

I assumed you were trying to monitor screen changes in another application where you did not know the code. If you can access the code that contains the pushScreen calls, without a doubt I think you should use Peter's suggestion. I believe placing PageAd.next() after all PushScreen() methods should do the trick.

 

Scott