I have an application with 2 Views and 2 Presenters. I have a fixed top menu with a search box using a SuggestionBox. I get the suggestion list from database, and I load it into the MultiWordSuggestOracle of the SuggestionBox, using addAll(..) method.
I'm using UiBinder, and it is my code:
@UiField (provided=true) SuggestBox searchEntriesSuggestBox;
MultiWordSuggestOracle oracle;
public MenuBar() {
oracle = new MultiWordSuggestOracle();
searchEntriesSuggestBox = new SuggestBox(oracle);
initWidget(uiBinder.createAndBindUi(this));
}
public void loadUserEntries(Collection<String> entries){
oracle.clear();
oracle.addAll(entries);
}
The first time that I run the app, I load the suggestions using loadUserEntries(). The problem is when I change my View (and Presenter), because the SuggestBox load an empty MultiWordSuggestOracle, so I have to load the suggestions again, every time I change my Presenter.
I would like to maintain the suggestions (the MultiWordSuggestOracle) as a global variable during the life of the app, to avoid using the loadUserEntries method.
I tried defining a global variable MultiWordSuggestOracle, and each time that I have to load the Presenter, create a new SuggestBox with my global MultiWordSuggestOracle. But it didn't work.
Any idea?
Thanks.
The problem is that each view uses its own instance of the MenuBar because each view creates its own.
There are different ways to solve your problem, you can use multiple ActivityManagers
like it is explained in this post, or you could have a fixed part in your UI with the MenuBar out of your MVP display, or a simple solution is to use a static variable and make the load method static (call this method just once).
@UiField (provided=true) SuggestBox searchEntriesSuggestBox;
static MultiWordSuggestOracle oracle = null;
public MenuBar() {
if (oracle == null) {
oracle = new MultiWordSuggestOracle();
}
searchEntriesSuggestBox = new SuggestBox(oracle);
initWidget(uiBinder.createAndBindUi(this));
}
public static void loadUserEntries(Collection<String> entries){
oracle.clear();
oracle.addAll(entries);
}