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.
launchCount
instance field.setLaunchCount
method that takes an int
argument and assigns it to the launchCount
instance field.@Inject
annotation to the setLaunchCount
method.@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; }
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.
3.140.188.16