gwtuibindergwt-activities

GWT Activities: Widget added to view again when using browser back button


The way I understand it, if I want to add widgets dynamically to a view created with UIBinder, I would do that in the start method of the activity that is the presenter for that view.

Here's my code:

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    view = clientFactory.getDashboardView();
    view.setPresenter(this);

    ArrayList<Department> deps = ModelFactory.getDepartments();
    view.passData(deps); // Correct?
    panel.setWidget(view.asWidget());

}



public void passData(ArrayList<Department> departments) {

TextCell text = new TextCell();
CellList<String> cellList = new CellList<String>(text);
String[] departmentNames = new String[departments.size()];
for (int i = 0; i < departments.size(); i++) {
    departmentNames[i] = departments.get(i).getName();
}
cellList.setRowData(Arrays.asList(departmentNames));
departmentsDiv.add(cellList);

}

It works. However, when using the back button to navigate to the previous place and back, the widget is added again.

How do I handle this correctly?


Solution

  • You have two options:

    1. If you don't want to refresh the data on each visit to this view, you need to add a flag to the view to tell if the data has been already populated. Then, when this view is visited again, your activity should call view.passData(deps); only if the flag is set to false. After the data is loaded, set the flag to true.

    2. If you do want to refresh the data on each visit, call departmentsDiv.clear() before adding a new CellList.

    NB: A better approach is to create your CellList once, when the view is displayed for the first time, and then only call setRowData when the new data is available.