活动处理程序不会被删除

时间:2011-11-01 21:45:03

标签: gwt gwt-mvp

我正在努力加快使用GWT活动和地点。我正在使用最初在此商品blog post上找到的一些源代码进行测试。

我发现在bind()期间添加的处理程序似乎永远不会删除。我对Activity javadoc的一点理解让我觉得它们应该在调用Activity的onStop()方法时自动删除。

  

它注册的所有事件处理程序都将在此之前删除   方法被称为。

但每次单击一个按钮时,相应的处理程序将被调用n + 1次。

我错过了什么?如果我能提供更多信息,请告诉我。

以下是代码中的相关摘录:

public class ContactsActivity extends AbstractActivity {

private List<ContactDetails> contactDetails;
private final ContactsServiceAsync rpcService;
private final EventBus eventBus;
private final IContactsViewDisplay display;
private PlaceController placeController;

public interface IContactsViewDisplay {
    HasClickHandlers getAddButton();
    HasClickHandlers getDeleteButton();
    HasClickHandlers getList();
    void setData(List<String> data);
    int getClickedRow(ClickEvent event);
    List<Integer> getSelectedRows();
    Widget asWidget();
}

public ContactsActivity(ClientFactory factory) {
    GWT.log("ContactActivity: constructor");

    this.rpcService = factory.getContactServiceRPC();
    this.eventBus = factory.getEventBus();
    this.display = factory.getContactsView();
    this.placeController = factory.getPlaceController();
}

@Override
public void start(AcceptsOneWidget container, EventBus eventBus) {
    GWT.log("ContactActivity: start()");

    bind();
    container.setWidget(display.asWidget());
    fetchContactDetails();

}

public void bind() {

    GWT.log("ContactActivity: bind()");

    display.getAddButton().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("Add button clicked");
            ContactsActivity.this.placeController.goTo(new NewContactPlace(""));
        }
    });

    display.getDeleteButton().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("ContactActivity: Delete button clicked");
            deleteSelectedContacts();
        }
    });

    display.getList().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("ContactActivity: List clicked");
            int selectedRow = display.getClickedRow(event);

            if (selectedRow >= 0) {
                String id = contactDetails.get(selectedRow).getId();
                ContactsActivity.this.placeController.goTo(new EditContactPlace(id));
            }
        }
    });
}

2 个答案:

答案 0 :(得分:7)

通过注册的活动。传递给EventBus的{​​{1}}将在调用AbstractActivity#start()时取消注册。但是,在上述onStop()方法中注册的事件处理程序不是通过bind()注册的,并且对抽象基类不可见。您需要自己取消注册:

EventBus

答案 1 :(得分:0)

我发现最好在视图中处理注册 - 让它负责只为每个按钮保持一次点击操作。

而不是:

 for (var i = 0; i <= data.length - 1 ; i++)
        {
           thisfill = fill;
                thisstroke = stroke;
                var styles = [new ol.style.Style({
                    image: new ol.style.Circle({
                        fill: thisfill,
                        stroke: thisstroke,
                        radius: 5
                    }),
                    fill: thisfill,
                    stroke: thisstroke
                })];

                customBldgLayerDC = new ol.layer.Vector({
                    source: new ol.source.Vector({
                        features: [feature],
                    }),
                    style: styles,
                });
                map.addLayer(customBldgLayerDC);
        }

..并在活动中链接到此:

class View {
    Button commitButton;

    public HasClickHandlers getCommit () {return commitButton;}
}

在视图中执行此操作:

view.getCommit.addClickHandler(new Clickhandler()...

活动:

    class View {
        private Button commitButton;        
        private HandlerRegistration commitRegistration = null;

        public void setCommitHandler (ClickHandler c) {
            commitRegistraion != null ? commitRegistration.removeRegistration ();
            commitRegistration = commitButton.addClickHandler (c);
        }
    }

希望有所帮助。