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
parkssoft
Posts: 8
Registered: ‎12-03-2009
My Device: 8310
Accepted Solution

How to refresh a screen with Updated Fields

I am a writing my first BB App and having trouble with refreshing screen objects(among other things).

 

I have an application with a main screen that calls an options screen.  The Options screen allows the user to change the units system, using a persistent store. 

 

Upon return to the main calc screen, the main calc screen to have updated text to reflect the change in units.  I see from other posts that I can use invalidate(), but I don't understand where to put invalidate() in the code.

 

On initial load, the main screen needs to get the persistent metric flag before setting the label fields with SetUnitLabels().

 

Choosing the Options from the menu pushes the options screen.

 

Popping the options screen and pushing the calcscreen returns to the main screen, and needs to call the  SetUnitLabels() method again to update the field text.

 

// This is the constructor in the CalcScreen, the main screen

 public CalcScreen() {

 

     super();

     boolIsMetric=myOptions.getBoolMetricFlag();  // retrieve metric flag to set screen text
   
    addScreenItems(); // adding visual elements to the screen

    calcScreen = this.getScreen();  // I get the name of this screen to use later

}

 

// This options method pushes the options screen:

public void options() {
      ClearScreen();
      UiApplication.getUiApplication().popScreen(calcScreen);
      UiApplication.getUiApplication().pushScreen(new OptionScreen(calcScreen));
     } 

 

// This goback method on the options screen returns to the main calcscreen:

 public void goback() {
        UiApplication.getUiApplication().popScreen(this);
        UiApplication.getUiApplication().pushScreen(toScreen);
    };

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

I have tried to use the following invalidate() runnable, but I don't know where to put it so that it get's called.

 

UiApplication.getUiApplication().invokeLater (new Runnable() {
    public void run()
    {
        SetUnitLabels();           //Perform screen changes here.

        calcscreen.invalidate();
    }
});

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

I am sure the answer is something simple, but I am just not getting it.

Thanks for your help.

Please use plain text.
Developer
pfiffi78
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: How to refresh a screen with Updated Fields

Try to overwrite the close() method of your Options screen like

 

public void close() {

   //put here the invokeLater code

 

   super.close();

}

 

If that doesn't work you could overwrite the CalcScreen's onUIEngineAttached(boolean) method and do

 

if(attached) {

  invalidate()

}

 

See what works best. The first one, should be only called when you close the Options screen. The second approach would be called whenever the CalcScreen becomes the visible screen. If the repaint of that screen isn't expensive than it's ok.

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to refresh a screen with Updated Fields

Invalidate() will only repaint the Screen.  It should be repainted anyway when the Options screen is popped, so I'm not sure that this is your problem.  So say for example the Screen was displaying a BasicEditField which had the contents "5", then it will repaint the "5".  This is fine, but I wonder if after the Options screen, you really want the value to change some of the displayed values, say to change the BasicEditField to say "6".  So you need to actually refresh the values of the Fields on your screen, not just repaint the current values.

 

If I have a need to so that, then I use a pushModal for the screen that is going to be displayed, then in the code after the pushModal, I update the screen Fields to match what might have been changed by the Options screen.  Does that make sense?

Please use plain text.
New Developer
parkssoft
Posts: 8
Registered: ‎12-03-2009
My Device: 8310

Re: How to refresh a screen with Updated Fields

I don't know if I follow you. Would I change the method that pushes the options screen like

 

public void options() {
      ClearScreen();
      UiApplication.getUiApplication().popScreen(calcScreen);
      UiApplication.getUiApplication().pushModal(new OptionScreen(calcScreen));

     

     // then put code here that changes calcscreen values

      // and invalidate();
     } 

 

It took me a long time to get the pushing and popping of the screens to work, but I could try a change.

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to refresh a screen with Updated Fields

From the look of your code, when you exit the Options Screen, you push a replacement, so the code I have suggested might not work - it expects a different screen management technique.  . 

 

Generally I have a start screen for the application, that is always there, From there I push the next screen, and when the user has finished with that screen, I pop it, which will redisplay the screen below.  You can have many layers of screens like this.

 

Now when I go into my Options screen, the processing code will just do a pushModal, as you have done, then the code following the push modal will be executed when the Options screen 'pop's.  So at that point I can update any values on the current screen. 

 

This is pretty much what you have suggested, but you don't need to pop the calcScreen because you want it displayed when the Options Screen is popped, so it might as well stay on the Display Stack. 

 

Does this make sense?

Please use plain text.
New Developer
parkssoft
Posts: 8
Registered: ‎12-03-2009
My Device: 8310

Re: How to refresh a screen with Updated Fields

You make a lot of sense, but I couldn't get the Calc screen to disappear for the Options screen without Popping it.  I think I'll try again though, as that's probably a better way to do it.

 

So, after I pop the Options screen, the Calc screen should still be there and then I call invalidate to repaint it with the changed values?

 

 

Please use plain text.
Developer
pfiffi78
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: How to refresh a screen with Updated Fields

[ Edited ]

To follow up on peter's reply:

 

Screens in BB apps form a stack, so when your app starts you display a main screen, the first in the app's screen stack, from there  you can push another screen. There are two ways to do that: either with pushScreen() or pushModal(). The difference is that in the first case, the calling code won't wait and continue. In the second instance, the calling code will block till the pushed screen is being closed. 

 

So all you need to do is something like this:

 

In you CalcScreen have a method that creates an instance of your Options screen and then call pushModel(optionsScreen). Create your OptionsScreen with Screen.DEFAULT_CLOSE when calling the super() constructor. This will give the screen default closing behaviour when the user presses the the ESC key, which in turn automatically pops the screen from the app's screen stack. After that happened, the method in your CalcScreen by which you have pushed the OptionScreen commences execution where you then can update the values of your fields.

 

 

public void showOptionsScreen() {
  OptionsScreen optionScreen = new OptionsScreen();
  UiApplication.getUiApplication().pushModal(optionScreen);

  //the following code part is only executed after the Option Screen has been popped from the stack, 
  //ie after it has been closed (by the user)

  // Now here place the code that updates the values of your fields on your CalcScreen
  // Let's say you have a 'Size' field then do something like this

  sizeField.setValue(calculateSizeValue()); 

  //whereas calculateSizeValue() takes the new unit settings made by the user into consideration

}

 

 

 

As peter pointed out, a call to invalidate() won't update any values it just repaints the fields with the values you originally set. You will need to explicitly set the new values on the fields and that will trigger an automatic repaint of that field.

 

Hope that clarifies things a bit.

Please use plain text.
New Developer
parkssoft
Posts: 8
Registered: ‎12-03-2009
My Device: 8310

Re: How to refresh a screen with Updated Fields

Thant's a big help. 

 

I have a method SetUnitValues() to run when after I pop the Options Screen.  It changes the text in about 7 fields to match the change in units system.  After I call SetUnitValues(), what tells the main screen know it's time to check to see if any of it's fields have changed and pick up the new values?

 

Thanks!

Please use plain text.
Developer
pfiffi78
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: How to refresh a screen with Updated Fields

Not quite sure what you're asking now but after you have updated the values of your fields that are added to the screen, the screen should update as you updated the values of the fields. 

Please use plain text.
New Contributor
turningchapters
Posts: 2
Registered: ‎11-22-2010
My Device: Not Specified

Re: How to refresh a screen with Updated Fields

Hi, i have problems in refreshing my screen too.

 

public void fieldChanged(Field field, int context) {
			if (field == clearButtonField) {
				clear();
			} else if (field == searchButtonField) {
				search();
				UiApplication.getUiApplication().invokeLater (new Runnable() {
				    public void run() 
				    {
				    	delete(tabArea);
						tabArea = displayTab2();
						add(tabArea);
				     tabArea.invalidate();
				    }
				});
		}

			}
	
		public void clear() {
			empField.setText("");
		}

		public void search() {
			if (empField.getTextLength() == 0) {
				Dialog.alert("Enter EmployeeID to Search!");
			} else if (empField.getTextLength() != 4) {
				Dialog.alert("Enter 4 digits!");
			} else {
				Z01HCM_WSLEAVERECORD1 stub = new Z01HCM_WSLEAVERECORD1_Stub();
				try {
					Z01HcmLeaverecordfmResponse output = stub
							.z01HcmLeaverecordfm(empField.getText(), "1");
					System.out.println(output);

					leaveCode01 = output.getOutLeavecode();
					leaveCode01Year = output.getOutYear();
					leaveCode01Total = output.getOutTotal();
					leaveCode01Taken = output.getOutTaken();
					leaveCode01Left = output.getOutLeft();

					output = stub.z01HcmLeaverecordfm(empField.getText(), "2");
					System.out.println(output);

					leaveCode02 = output.getOutLeavecode();
					leaveCode02Year = output.getOutYear();
					leaveCode02Total = output.getOutTotal();
					leaveCode02Taken = output.getOutTaken();
					leaveCode02Left = output.getOutLeft();

					output = stub.z01HcmLeaverecordfm(empField.getText(), "3");
					System.out.println(output);

					leaveCode03 = output.getOutLeavecode();
					leaveCode03Year = output.getOutYear();
					leaveCode03Total = output.getOutTotal();
					leaveCode03Taken = output.getOutTaken();
					leaveCode03Left = output.getOutLeft();

					output = stub.z01HcmLeaverecordfm(empField.getText(), "4");
					System.out.println(output);

					leaveCode04 = output.getOutLeavecode();
					leaveCode04Year = output.getOutYear();
					leaveCode04Total = output.getOutTotal();
					leaveCode04Taken = output.getOutTaken();
					leaveCode04Left = output.getOutLeft();

					output = stub.z01HcmLeaverecordfm(empField.getText(), "5");
					System.out.println(output);

					leaveCode05 = output.getOutLeavecode();
					leaveCode05Year = output.getOutYear();
					leaveCode05Total = output.getOutTotal();
					leaveCode05Taken = output.getOutTaken();
					leaveCode05Left = output.getOutLeft();

					output = stub.z01HcmLeaverecordfm(empField.getText(), "6");
					System.out.println(output);

					leaveCode06 = output.getOutLeavecode();
					leaveCode06Year = output.getOutYear();
					leaveCode06Total = output.getOutTotal();
					leaveCode06Taken = output.getOutTaken();
					leaveCode06Left = output.getOutLeft();

				} catch (Exception ex) {
					String msg = ex.toString();
					System.out.println(msg);
			
				}
			}
		}

 

 

 

 

 

How do i actually refresh my screen with the information retrieved from the web service onces i click on the search button? I want to display a treefieldlist

THANKS FOR YOUR HELP IN ADVANCED! CHEERS! :smileyhappy:

Please use plain text.