Time for action – responding to preference changes

In order to react when preferences are changed, it is necessary to inject the preference value as part of a method call. When the preference value is changed, the method will be invoked again with the new value.

  1. Remove the annotations associated with the launchCount instance field.
  2. Create a setLaunchCount method that takes an int argument and assigns it to the launchCount instance field.

    Tip

    Type setL and press Ctrl + space to suggest creating this method automatically.

  3. Add the @Inject annotation to the setLaunchCount method.
  4. Add the @Preference(nodePath = "com.packtpub.e4.clock.ui", value = "launchCount") annotation to the method argument. The resulting method will look like this:
    int launchCount;
    
    @Inject
    public void setLaunchCount(
     @Preference(nodePath = "com.packtpub.e4.clock.ui",
       value = "launchCount") int launchCount) {
      this.launchCount = launchCount;
    }
  5. Run the target Eclipse instance, and open the Time Zone Tree View. In the Console view, there should be a Launch count is: message with the same value as before.

What just happened?

Instead of the value being injected when the view is created, the value is set by calling the method. This has the same effect as before, but this time, if the preference value subsequently changes, the value will be re-injected by calling the method again. If the preference affects the way the component is shown, the method can trigger the user interface to be updated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.188.16