javagwtgwtp

GWTP Presenter and View retaining old values


I am using GWTP and all my presenters extend Presenter class. If I navigate from A to B and then back to A, then instead of the new values, the old values in the form A are still displayed as if it was displaying a copy of the old values.

How can I make the app create new instances of Presenters to avoiding displaying old values?


Solution

  • Presenter and Views are usually expensive to instantiate, so GWTP recommend to use both as a singleton, the view is unnecessary bc the presenter will keep the instance so it behaves like an inner-singleton. In that case, your current case, just reset the presenter state on one of the life cycle phases "prepareFromRequest", "onReveal" or "onReset" depending on how your view works.

    Note that doing this is much easier than it looks, if your view is not in sync with your presenter, which should be your case, just move the code from the less-frequently-updated life-cycle (ex. construction or onBind) to a more-frequently-updated (ex. onReveal or onReset) then, whenever you navigate your presenter and the view is showed, this will be correctly updated and the singleton problem will get irrelevant.

    If you really want to create a non-singleton presenter you can do the binding manually instead of using AbstractPresenterModule#bindPresenter. This might be a bit dangerous if you try to reference the presenter in multiple places, and also the presenter will be the same during resets, but I think it will be a new instance on each reveal.