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
New Developer
kentt
Posts: 8
Registered: ‎12-16-2009
My Device: Not Specified
Accepted Solution

Closing a popup screen to be reopened later

[ Edited ]

Here's the basic idea. I have screenA, where the user can select a button.. Upon selection, ScreenB (a popup) is pushed, where the user chooses between sub-options. Upon choosing they return to screenA. If they choose another button screenB should show again. However, everytime I try to reshow screenB I get an illegalStateException.

I think my problem is that I'm not closing screenB correctly (the methods I tried, I wrote down at the end of the code). Here is some sample code that has the basic idea down.

 

 

 

/*
 * Test.java
 */
package test;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;



public class Test extends UiApplication
{
	private FieldListener listener = new FieldListener();
	public static TestScreen testScreen;
	
	public static void main (String args[])
	{		
		Test app = new Test();
		app.enterEventDispatcher();
	}
	
	public Test()
	{
		//display a new screen
		testScreen = new TestScreen();
		UiApplication.getUiApplication().pushScreen(testScreen);
	}
	ButtonField bf1 = new ButtonField(
			"Hello World",ButtonField.CONSUME_CLICK);
	
	public class TestScreen extends MainScreen 
    {
		public TestScreen() 
		{
			super();
			VerticalFieldManager vfm = 
				new VerticalFieldManager();
			bf1.setChangeListener(listener);
			add(bf1);
			add(vfm);
		}
    }
	class FieldListener implements FieldChangeListener
	{
		public void fieldChanged(Field button, int context)
		{
			TestPopupScreen.popup = new TestPopupScreen();
			UiApplication.getUiApplication().pushScreen(
					TestPopupScreen.popup); 
		}
	}
	
	
}

 

/*
 * TestPopupScreen.java
 */
package test;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;


public class TestPopupScreen extends PopupScreen 
{
	public static TestPopupScreen popup;
	private static VerticalFieldManager vfm = 
		new VerticalFieldManager();
	private FieldListener listener = new FieldListener();
		
	public TestPopupScreen()
	{
		super(vfm,Field.FOCUSABLE);
		ButtonField bf = new ButtonField(
				"Hello Again",ButtonField.CONSUME_CLICK);
		bf.setChangeListener(listener);
		add(bf);
	}
	
	class FieldListener implements FieldChangeListener
	{
		public void fieldChanged(Field button, int context)
		{
			//What is the proper way to close the screen so it 
			//can be relauched later?
			
			//popup.close();// IllegalStateException
			
			//UiApplication.getUiApplication().popScreen(popup);
			//gives IllegalStateException
			
			/*
			 * RuntimeException
			 * UiApplication.getUiApplication().pushScreen(Test.testScreen); 
	         * UiApplication.getUiApplication().popScreen(popup);
			 */
	        
		}
	}
}

Thanks in advace for any help. It's very much appreciated.

take care,

kentt

 

 

 

 

 

 

 

 

 

 

 

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

Re: Closing a popup screen to be reopened later

[ Edited ]

Welcome!

 

Haven't looked seriously at your problem, but I think your popup screen should be recoded as follows.  Try it, I think your problem will go away. 

 

In general I would be very careful about the use of static variables.  You seem to use them in places where they are not needed. 

 

public class TestPopupScreen extends PopupScreen
                             implements FieldChangeListener
{
	private VerticalFieldManager vfm = 
		new VerticalFieldManager();
		
	public TestPopupScreen()
	{
		super(vfm,Field.FOCUSABLE);
		ButtonField bf = new ButtonField(
				"Hello Again",ButtonField.CONSUME_CLICK);
		bf.setChangeListener(listener);
		add(bf);
	}
	
	public void fieldChanged(Field button, int context)
	{

		UiApplication.getUiApplication().popScreen(this);

	}
}

 

New Developer
kentt
Posts: 8
Registered: ‎12-16-2009
My Device: Not Specified

Re: Closing a popup screen to be reopened later

Hi peter_strange.  Thanks for the advice about static variables.   I'll be mindful of that.

 

As far as the rest of the code goes, I'm a bit confused on how fieldChanged is fired?

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

Re: Closing a popup screen to be reopened later

Apologies, in my code, I have a line commented out.  I've fixed the code.  Hoepfully it makes more sense now and answers your question.

New Developer
kentt
Posts: 8
Registered: ‎12-16-2009
My Device: Not Specified

Re: Closing a popup screen to be reopened later

No not quite.  I had already uncommented that line.  What I am confused about is "bf.setChangeListener(listener);" part.  I don't understand how that is implemented.

Thanks for looking,

kentt

New Developer
kentt
Posts: 8
Registered: ‎12-16-2009
My Device: Not Specified

Re: Closing a popup screen to be reopened later

Okay, so this is what I was trying, but now the fieldChanged() function never runs.  I've been poking around on the the internet and trying to see why not but it's baffeling me. 

Thanks for looking.  

 

 

public class TestPopupScreen extends PopupScreen implements FieldChangeListener 
{
	private static VerticalFieldManager vfm = 
		new VerticalFieldManager();
		
	public TestPopupScreen()
	{
		super(vfm,Field.FOCUSABLE);
		ButtonField bf = new ButtonField(
				"Hello Again",ButtonField.CONSUME_CLICK);
		bf.setChangeListener(this);
		vfm.add(bf);
		add(vfm);
	}

	public void fieldChanged(Field field, int context) 
	{	
//this never runs
 UiApplication.getUiApplication().popScreen(this); } }

 

/*as an off topic note, in Opera 10.10, you cannot insert code into your post*/

 

Developer
dynamicdhaka
Posts: 101
Registered: ‎12-10-2009
My Device: styile 9670

Re: Closing a popup screen to be reopened later

 I have find out your problem. the main reason was for your problem is you can not push same screen object more than once in stack. When you click second hello world button you are pushing the first screen again to show that.this is wrong. you can not do this ..i have attached the code.

 

package test;


import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;



public class Test extends UiApplication
{
    private FieldListener listener = new FieldListener();
    public static TestScreen testScreen;
    
    public static void main (String args[])
    {        
        Test app = new Test();
        app.enterEventDispatcher();
    }
    
    public Test()
    {
        //display a new screen
        testScreen = new TestScreen();
        UiApplication.getUiApplication().pushScreen(testScreen);
    }
    ButtonField bf1 = new ButtonField("Hello World",ButtonField.CONSUME_CLICK);
    
    public class TestScreen extends MainScreen
    {
        public TestScreen()
        {
            super();
            VerticalFieldManager vfm = new VerticalFieldManager();
            bf1.setChangeListener(listener);
            add(bf1);
            add(vfm);
        }
    }
    class FieldListener implements FieldChangeListener
    {
        public void fieldChanged(Field button, int context)
        {
            TestPopupScreen.popup = new TestPopupScreen();
            UiApplication.getUiApplication().pushScreen(TestPopupScreen.popup);
        }
    }
}

 

/////////////

package test;

import test.Test.FieldListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;


public class TestPopupScreen extends PopupScreen
{
    public static TestPopupScreen popup;
    
    public TestPopupScreen()
    {
        super(new VerticalFieldManager(),Field.FOCUSABLE);
        ButtonField bf = new ButtonField("Hello Again",ButtonField.CONSUME_CLICK);
        bf.setChangeListener(listener);
        add(bf);
    }
    FieldChangeListener listener= new FieldChangeListener()
    {
    public void fieldChanged(Field field, int context) {
      
        UiApplication.getUiApplication().popScreen();
        
    }
    };

    
}

Mobile Application Advisor
New Developer
kentt
Posts: 8
Registered: ‎12-16-2009
My Device: Not Specified

Re: Closing a popup screen to be reopened later

Oh gosh.  Thank you so much.

Got that working and implemented it in my own program.

I'm very grateful that you took the time.

 

As a small side note, if someone else is looking  at this code, the last line should read UiApplication.getUiApplication().popScreen(poup);

 

Take care,

kentt

Developer
belberry
Posts: 265
Registered: ‎08-27-2009
My Device: Not Specified

Re: Closing a popup screen to be reopened later

good solution thank u one and all

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

Re: Closing a popup screen to be reopened later

To make sure there are no unexpected side effects with multiple instances of this code, 

 

1) I would recommend removing the static TestPopupScreen in TestPopupScreen, and

 

2) the fieldChanged method in TestPopupScreen should read

 

UiApplication.getUiApplication().popScreen(this);

 

not

 

UiApplication.getUiApplication().popScreen(popup);