Reputation: 2861
I want to remove a GWT event handler the first time I receive an event. I also want to avoid polluting my class with tracking registration objects that aren't really necessary. I currently have it coded as:
final HandlerRegistration[] registrationRef = new HandlerRegistration[1]; registrationRef[0] = dialog.addFooHandler(new FooHandler() { public void onFoo(FooEvent event) { HandlerRegistration removeMe = registrationRef[0]; if(removeMe != null) { removeMe.removeHandler(); } // do stuff here } });
but the use of registrationRef makes the code less readable. Is there a better way to do this without adding variables to my class?
Upvotes: 12
Views: 10091
Reputation: 15331
I'd just make the HandlerRegistration
object a field of the enclosing class, that way you won't be bothered by the compiler and it's more "elegant" than shuffling arrays and stuff:
public class TestWidget extends Composite {
//...
HandlerRegistration handler;
public TestWidget() {
// ...
handler = button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
// ...
handler.removeHandler();
}
});
}
}
Upvotes: 12