Search code examples
blackberryblackberry-jde

What's the difference between Screen.invalidate() and Screen.invalidateLayout()


void invalidate() 
      Invalidates the entire screen.


void invalidateLayout() 
      Invalidates this screen's layout (including all controlled fields).

The javadoc doesn't help much. Could you tell me why do we need invalidateLayout()?


Solution

  • Depending on how you build your UI, you may be interested in invalidating, or updating layout, when you want the display to change.

    If you have a UI that's composed of standard Field objects, like TextField, or ButtonField, or BrowserField, without much customization, then the work of defining how the UI looks is mostly in how those fields are laid out. Layout refers to setting the size of fields, and their position.

    Sometimes, you'll have a UI where you need to deal at a lower level. For example, you may do some custom painting, which involves drawing lines, filling areas, drawing gradients, etc. This work is normally done in a Field object's paint() method. Sometimes, you also have drawing code in other places like drawFocus() or paintBackground().

    If you want to trigger repainting, which calls your custom paint() method, then you want to use invalidate() to make that happen.

    If you want to trigger a new layout, which arranges child fields with certain sizes, or positions, then invalidateLayout() can be used for that.

    However, I will note that invalidateLayout() is a method in the Screen class. If you use a RIM Screen subclass, like MainScreen or FullScreen, then they come with their own top level delegate Manager objects. I believe that calling Screen.invalidateLayout() will trigger the sublayout() method on that built-in Manager to be called. That may not be what you really want.

    Unless you implement your own Screen subclass like this:

    public class MyScreen extends Screen {
    
       public MyScreen() {
           super(new MyCustomManager());
       }
    }
    

    you probably aren't implementing the code (including sublayout()) for that delegate manager yourself.

    I have found that when I want to force sublayout() to be called again, I trigger that with the updateLayout() method, not invalidateLayout(). Also, as opposed to invalidateLayout(), which is a method of the Screen, updateLayout is available for all Manager and Field classes, so you can choose just the specific part of the UI element tree that you want to refresh. But, that's just my experience.

    Here's some comments on the BlackBerry forums on this topic