Search code examples
androidmvpmosby

MVP: how to notify view about changes after de-attach


Let's imagine situation:

  1. User click on 'Login' button and Fragment(View) call Presenter's method doLogin().
  2. Presenter starts some async work and now Boom! app is closed(moved to recent apps)
  3. Presenter survives and async work is still happening.
  4. Async work finished while app was in the background.
  5. User came back to app, but he doesn't see any notification that work is finished as view was de-attached:

            if(isViewAttached()) {
                getView().setLoaded(workResult);
            }
    

And I want to fix it. The only way that I see is to use Queue<MessageToView> and when View has attached again, execute every "Message".

I think that there is a library that can handle my case. So, is it? Or what pattern can I use?


Solution

  • See github pages FAQ section:

    Can the Presenter and its view be out of sync during a screen orientation change?

    Excellent question. Mosby assumes that all interaction from Presenter with the View happens on android’s main UI thread. Hence the answer is no that cannot happen since screen orientation changes are executed on the main UI thread as well. So either is a screen orientation executed completely (view reattached) or the presenter invokes the views method after view is reattached since both run on main UI thread or the presenter invokes the views methods before starting screen orientation change.

    So as long as your Presenter invokes View methods on main UI thread everything works out of the box.