MVP:如何在取消附加后通知有关更改的视图
MVP: how to notify view about changes after de-attach
让我们想象一下情况:
- 用户单击 'Login' 按钮,然后
Fragment
(View
) 调用 Presenter
的方法 doLogin()
。
Presenter
开始一些异步工作,现在 Boom! 应用已关闭(移至最近的应用)
Presenter
幸存下来,异步工作仍在进行。
- 异步工作在应用程序处于后台时完成。
用户返回应用程序,但他没有看到任何工作已完成的通知,因为视图已取消附加:
if(isViewAttached()) {
getView().setLoaded(workResult);
}
我想修复它。我看到的唯一方法是使用 Queue<MessageToView>
并且当 View
再次附加时,执行每个 "Message".
我认为有一个图书馆可以处理我的情况。那么,是吗?或者我可以使用什么模式?
尝试使用 Fragment 的 onResume()
生命周期方法,然后调用 presenter.updateViews()
参见 github 页 FAQ 部分:
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.
因此,只要您的 Presenter 在主线程 UI 上调用 View 方法,一切都开箱即用。
让我们想象一下情况:
- 用户单击 'Login' 按钮,然后
Fragment
(View
) 调用Presenter
的方法doLogin()
。 Presenter
开始一些异步工作,现在 Boom! 应用已关闭(移至最近的应用)Presenter
幸存下来,异步工作仍在进行。- 异步工作在应用程序处于后台时完成。
用户返回应用程序,但他没有看到任何工作已完成的通知,因为视图已取消附加:
if(isViewAttached()) { getView().setLoaded(workResult); }
我想修复它。我看到的唯一方法是使用 Queue<MessageToView>
并且当 View
再次附加时,执行每个 "Message".
我认为有一个图书馆可以处理我的情况。那么,是吗?或者我可以使用什么模式?
尝试使用 Fragment 的 onResume()
生命周期方法,然后调用 presenter.updateViews()
参见 github 页 FAQ 部分:
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.
因此,只要您的 Presenter 在主线程 UI 上调用 View 方法,一切都开箱即用。