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.

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.

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?

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.

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.

Re: How to refresh a screen with Updated Fields

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?