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
Highlighted
Contributor
Posts: 27
Registered: ‎01-21-2011
My Device: Bold 9900
My Carrier: Cellcom
Accepted Solution

Closing global screen issue

Hey,

 

My application is sending a global event to trigger a global screen (pushed with invokeLater).

I'm having 2 problems:

  1. closing the global screen
  2. Show a Dialog.inform screen

More details:

  1. When i'm closing the app. with my 'cancel' button or by the close() method the screen disapears and then pops up again
  2. When I'm pressing the 'Add' button it runs the addContact() method and adds the contact, altough it never shows the Dialog.inform( caller + " is now a contact" )
    It's also closing the global screen and the shows it again like when I'm using the cancel button..

Here is my code:

 

public class AddToContactsScreen extends MainScreen implements FieldChangeListener
{
	private final int LBL_COLOR = Color.LIGHTSKYBLUE;  
	private final int BG_COLOR = Color.BLACK;
	Caller caller;
	
	VerticalFieldManager vfm;
	
	/**
	 * 
	 */
	public AddToContactsScreen( Caller caller )
	{
		super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);

		setBackground(BackgroundFactory.createSolidTransparentBackground(BG_COLOR, 200));
		this.caller = new Caller(caller);
		initFields();
	}
	
	private void initFields()
	{
        VerticalFieldManager vfmCentre = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH);
        
        String lblTxt = "Add " + caller + " to your contacts?"; 
        
		LabelField lbl = new LabelField( lblTxt , LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER )
		{
	        public void paint(Graphics g) 
	        {
	        	g.setColor( LBL_COLOR );
	            super.paint(g);
	        }
		};
		lbl.setFont( Font.getDefault().derive(Font.BOLD, 30) );
        ButtonField addBtn = new ButtonField("Add", ButtonField.FIELD_HCENTER);
        ButtonField cancelBtn = new ButtonField("Cancel", ButtonField.FIELD_HCENTER);
        
        /* Add button's listeners */
		addBtn.setCookie( new String("addBtn") );
		cancelBtn.setCookie( new String("cancelBtn") );
		addBtn.setChangeListener(this);
		cancelBtn.setChangeListener(this);
        /* -- */
		
        vfmCentre.add(lbl);
        vfmCentre.add(addBtn);
        vfmCentre.add(cancelBtn);
        
        this.add(vfmCentre);
	}

    protected void paint(Graphics graphics) 
    {
        super.subpaint(graphics);
    }
    
    public void fieldChanged(Field field, int context)
    {
    	if( field.getCookie().equals("addBtn") )
    	{
    		addContact();
    	}
    	else if( field.getCookie().equals("cancelBtn") )
    	{
    		close();
    	}
    }
    
	private void addContact()
	{
		try
	  	{
			BlackBerryContactList contactList = (BlackBerryContactList) 
			PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.WRITE_ONLY);
			
			BlackBerryContact contact = (BlackBerryContact) contactList.createContact();
			 
			String[] name = new String[ contactList.stringArraySize( BlackBerryContact.NAME ) ];
			name[BlackBerryContact.NAME_GIVEN] = caller.getFirstName();
			name[BlackBerryContact.NAME_FAMILY] = caller.getLastName();
			contact.addStringArray(BlackBerryContact.NAME, BlackBerryContact.ATTR_NONE, name);
			contact.addString(BlackBerryContact.TEL, BlackBerryContact.ATTR_WORK, caller.getNumber() );
			    
			contact.commit();
		}
	  	catch (PIMException e)
	  	{
			e.printStackTrace();
		}
	  	
	  	Dialog.inform( caller + " is now a contact" );
	  	close();
	}  
    
    public boolean onClose()
    {
    	UiApplication.getUiApplication().popScreen( getScreen() );
    	return true;
    }
}

 Any ideas?

 

Thanks,

Rotem

Developer
Posts: 16,791
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Closing global screen issue

buttonfields should usually use the CONSUME_CLICK style, otherwise the action is executed AND a click is handled, with often strange side effects.
(for example, the dialog is clicked and vanishes)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 27
Registered: ‎01-21-2011
My Device: Bold 9900
My Carrier: Cellcom

Re: Closing global screen issue

Thanks!
We did one step with adding the CONSUME_CLICK style - Now the 'Cancel' button is working as needed and the screen closes.

The 'Add' button is still a problem..
The Dialog.inform won't pop and it is now never closes the screen.
1. Should I use Status.show instead the Dialog inform?
2. Should I put the popScreen in the addContact method instead calling to close() ?
Developer
Posts: 16,791
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Closing global screen issue

i am not totally sure what happens, but maybe you can comment out your onClose override, it could be the culprit (what is the reason for this code anyhow?)

btw, if you define your buttons in your screen you don't have to use get/setcookie to identify them in the changelistener, you can just use ==.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 27
Registered: ‎01-21-2011
My Device: Bold 9900
My Carrier: Cellcom

Re: Closing global screen issue

The reason for this code:

When there is an unidentified incoming call the application is searching the caller on our BES server and if the caller is found it shows the current screen on call disconnect. (So the user will be able to save the unidentified caller)

I commented the onClose and it is working great without it, altough the Dialog.inform / Staus.show still won't pop..

 

Any more ideas?

And how will identify the button if I won't use the cookie? just compare the field to the button's name?

For example:

if( field == "addBtn" )
{
    	addContact();
}

 ?

 

--

Rotem

 

Developer
Posts: 16,791
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Closing global screen issue

no, without any "", just field == addBtn.

a dialog is basically a popupscreen, maybe it gets undisplayed when you undisplay the global screen (i don't have much experience there, so its just a guess)

if you just want to notify the user you could try a global popupscreen instead (so a second global screen), or you could use a blocking variant like dialog.alert which blocks until the user pressed ok.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 27
Registered: ‎01-21-2011
My Device: Bold 9900
My Carrier: Cellcom

Re: Closing global screen issue

Hi again..

 

I'm still working on that global screen, and I made a few changes.

 

A brief update:

 

  1. The global screen pops and closes only after 2 clicks (add or cancel buttons)
    (The add button is functioning ok and adding the contact **bleep** needed..)
  2. The global dialog pops after the contact is added, but it has the same problem like the above global screen and need 2 clicks to close it self.

My code:

public class AddToContactsScreen extends MainScreen implements FieldChangeListener
{
	private int LBL_COLOR = Color.LIGHTSKYBLUE;  
	private int BG_COLOR = Color.BLACK;
	private Caller caller;
	private ButtonField addBtn, cancelBtn;
	
	/**
	 * 
	 */
	public AddToContactsScreen( Caller caller )
	{
		super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);

		setBackground(BackgroundFactory.createSolidTransparentBackground(BG_COLOR, 200));
		this.caller = new Caller(caller);
		initFields();
	}
	
	private void initFields()
	{
        VerticalFieldManager vfmCentre = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH);
        
        String lblTxt = "Add " + caller + " to your contacts?"; 
        
		LabelField lbl = new LabelField( lblTxt , LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER )
		{
	        public void paint(Graphics g) 
	        {
	        	g.setColor( LBL_COLOR );
	            super.paint(g);
	        }
		};
		lbl.setFont( Font.getDefault().derive(Font.BOLD, 30) );
        addBtn = new ButtonField("Add", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);
        cancelBtn = new ButtonField("Cancel", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);
        
        /* Add button's listeners */
		addBtn.setChangeListener(this);
		cancelBtn.setChangeListener(this);
        /* -- */
		
        vfmCentre.add(lbl);
        vfmCentre.add(addBtn);
        vfmCentre.add(cancelBtn);
        
        this.add(vfmCentre);
	}

    protected void paint(Graphics graphics) 
    {
        super.subpaint(graphics);
    }
    
    public void fieldChanged(Field field, int context)
    {
    	if( field == addBtn )
    	{
    		addContact();
    		UiApplication.getUiApplication().popScreen(this);
    		contactAddedMsg();
    		close();
    	}
    	else if( field == cancelBtn )
    	{
    		UiApplication.getUiApplication().popScreen(this);
    		close();
    	}
    }
    
	private void addContact()
	{
		.....Some code (working fine)
	}
	
	private void contactAddedMsg()
	{
		final String msg = caller + " is now a contact";
		final EncodedImage _icon = EncodedImage.getEncodedImageResource("img/bd_over.png").scaleImageToFill(100, 100);
		
        UiApplication.getUiApplication().invokeLater(new Runnable()
        {
            public void run()
            {
            	synchronized (Application.getEventLock())
            	{
            		Screen scr = new Dialog(Dialog.OK, msg, 1, _icon.getBitmap(), Dialog.NO_VERTICAL_SCROLL);
            		
            		UiEngine ui = Ui.getUiEngine();
            		ui.pushGlobalScreen( scr, 1, UiEngine.GLOBAL_QUEUE);
            	}
            }
        });
	}

 

Thanks!

Rotem

 

 

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

Re: Closing global screen issue

Put a break point in your button processing and check that the screen (this) is in fact the same screen.  What you are seeing looks like the screen is being pushed twice and it likely to be two different instances of the same screen.  . 

 

And check the code that pushes this screen to make 100% sure that it does not push it twice. 

Contributor
Posts: 27
Registered: ‎01-21-2011
My Device: Bold 9900
My Carrier: Cellcom

Re: Closing global screen issue

I added a System.out.println on the globalEventListener and I found out that the event has been triggered 5 times, and this is the reason for the multiple screens.

 

The code that triggers the global event:

    public void callDisconnected( int callId )
    {   
    	/*
    	 * If caller was identified -> allow the user to add him to contacts
    	 */
    	if( caller.isIdentified() )
    	{    	
    		// GUID = com.rreiss.besId.CallerToContactsListener
    		ApplicationManager.getApplicationManager().postGlobalEvent(0xa2f09ac81056548bL, 0, 0, new Caller( caller ), null);
    	}
    	
    	/**
    	 * @TODO
    	 * Testing purposes - remove block
    	 */
    	if( !caller.isIdentified() )
    	{    
    		Caller tstCaller = new Caller();
    		tstCaller.setFirstName("test");
    		tstCaller.setLastName("testlast");
    		tstCaller.setNumber("0524677963");
    		// GUID = com.rreiss.besId.CallerToContactsListener
    		ApplicationManager.getApplicationManager().postGlobalEvent(0xa2f09ac81056548bL, 0, 0, new Caller( tstCaller ), null);
    	}
    	/*
    	 * End of testing block
    	 */
    	
    	caller.resetCaller();
    }

In the code above I have 2 blocks to post globalEvent, one of them is only for testing with the simulator. (The first condition will never be true in a simulator..)

 

I'm also posting the eclipse console content, maybe it will help you to understand the problem:

[1659.906] EV_CALL_DISCONNECTED(6)
[1659.914] calldisc-no-call
[1659.914] dAct
[1659.937] callsmpt; switchbg=true
[1659.945] dInc
[1659.953] inc-nstop
[1659.992] CC: Disc,6
[1660.0] **** Event occurred - screen is being pushed ****
[1660.007] **** Event occurred - screen is being pushed ****
[1660.007] ISM: a=0; p= 0; t= 0
[1660.023] VM:PISVt=0,h=3980,id=d15a33128dfbb5d9
[1660.023] VM:ACGRt=1,c=0
[1660.031] VM:PISVt=1,h=3980,id=d15a33128dfbb5d9
[1660.031] VM:LNTDa=commit,t=1,p=net_rim_bb_phone_app,h=38
[1660.031] VM:PISVt=0,h=3980,id=d15a33128dfbb5d9
[1660.031] VM:LNTDa=commit,t=0,p=net_rim_bb_phone_app,h=38
[1660.07] PHONE: callId 6 stops listening.
[1660.07] **** Event occurred - screen is being pushed ****
[1660.07] noconf-rqstBG
[1660.07] AM: Foreground is requested: Bes(279)
[1660.078] JVM: bklt[1] @1660078: JvmBacklightSetTimeout 30
[1660.078] JVM: bklt[1]: setTimeout 30
[1660.085] Backlight -ICD
[1660.085] JVM: bklt[1] @1660085: SetPr Profile p=1 t=1 is already set.  IGNORED
[1660.085] JVM: bklt[1] @1660085: SetPr[2]=1
[1660.085] JVM: bklt[1] @1660085: SetPr Profile p=1 t=4 is already set.  IGNORED
[1660.085] JVM: bklt[1] @1660085: SetPr Profile p=1 t=8 is already set.  IGNORED
[1660.101] GS(createSurface): Temporarily promoting window size
[1660.132] GS(createSurface): Temporarily promoting window size
[1660.14] DTMFEcho thread start
[1660.25] GS(createSurface): Temporarily promoting window size
[1660.906] pulse off
[1660.906] **** Event occurred - screen is being pushed ****
[1660.906] **** Event occurred - screen is being pushed ****
[1660.906] AM: Foreground is set: Bes(279)
[1660.914] app-deac
[1660.914] Backlight -ACS
[1660.914] JVM: bklt[1] @1660914: SetSlPr Profile p=8 t=1 is already set.  IGNORED
[1660.914] JVM: bklt[1] @1660914: SetSlPr Profile p=8 t=2 is already set.  IGNORED
[1660.914] JVM: bklt[1] @1660914: SetSlPr Profile p=8 t=4 is already set.  IGNORED
[1660.914] JVM: bklt[1] @1660914: SetSlPr t=8, p=8
[1660.953] poppedactscrn
[1661.085] BRM:IDL+
[1661.085] BRM:SR+
[1661.085] ISM: a=0; p= 0; t= 0
[1661.085] GS(createSurface): Temporarily promoting window size

 The bolded lines are the System.out.println output..

 

Thanks,

Rotem

 

Developer
Posts: 16,791
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Closing global screen issue

check if you only add the phone listener once (and de-register it properly on shutdown), it is possible that old listeners remain.

if that does not help you have to check for the event to be unique, for example by ignoring an event with the same data.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter