How do I determine whether I am going "forward" or "backward" through my History in GWT?
I am looking at History and History JavaDocs in GWT and I notice that there is no way to tell whether the forward or backward button was pressed (either pragmatically or by the user). The "button press" is handled by your registered addValueChangeHandler, but the only thing passed to the handler is a string on your history stack. There is no indication as to whether the "Histo开发者_如何转开发ry" is moving "back" (using the back arrow button) or "forward" (using the right arrow button). Is there any way to determine this?
Sorry, you can't. And even if you could, there are browsers, like Firefox, that let the user "jump" back more than one page. So if you try to relying on relative "coordinates" instead of absolute, the navigation could break your app.
You can always append some kind of counter on your history token. It would not be hard, if you have only one history listener.
What you can do is have one UrlManager like this:
public class UrlManager implements ValueChangeHandler<String> {
public UrlManager() {
History.addValueChangeHandler(this);
}
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
}
}
Save every history token to a list.
If it is a completely new history token then add it to the list.
If it is the previous history token, then the back button has been pressed.
I suggest using a list to save the tokens and save an iterator that can move up and down the list. So initially it points at the end of the list. If the new token is the previous item on the list, then the back button has been pressed. If you're in the middle of the list (back a few times) and a new entry comes in (new link pressed), remove the rest of the list (can't go forward there anymore) and add the new one- and move iterator to that item.
You get the idea.
精彩评论