我可以在Fragment中注册MVP Presenter吗 [英] Can I register MVP Presenter inside Fragment
问题描述
我一直遵循 Google 重构我的应用程序.我有一个MainActivity和许多Fragments,为每个片段创建一个活动对我来说似乎有点混乱,所以我一直在考虑在片段中注册主持人.我看到的是每个片段都注册了自己的演示者,但我不确定它有多大错...:)
I've been following MVP design pattern provided by Google to refactor my application. I have one MainActivity and many Fragments and it seems little be messy for me to create an activity for every fragment, so I've been thinking to register presenter in fragment. What I'm seeing is that every fragment register its own presenter, but I'm not sure how much wrong it is... :)
这是我的主持人:
public class FirstPresenter implements FirstContract.Presenter {
private final FirstContract.View mView;
public FirstPresenter(FirstContract.View view) {
mView = view;
}
@Override
public void start() {
Log.e(TAG, "Start");
}
}
这是我的片段:
public class FirstFragment extends Fragment implements FirstContract.View {
private FirstContract.Presenter mPresenter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container
, Bundle savedInstanceState) {
...
// I register firstFragment's presenter here.
mPresenter = new FirstPresenter(this);
...
所以我的问题是,这是正确的方法吗?是否可以在Activity中将Presenter注册到Fragment中?如果不是正确的方法,是否有一些很好的例子可以处理一个活动和多个片段的MVP?
So my question is, is this right way? Can I register Presenter into Fragment instead in Activity? And if it is not the right way, is there some good example to handle MVP with one activity and multiple fragments?
谢谢你们, BR!
推荐答案
如您在 Google的示例( https://github.com/googlesamples/android-architecture ),Activities
创建Presenters
.同样,将Views
附加到Activity
和Presenters
以获得视图(Fragments
)作为参数.
As you can see in Google's samples (https://github.com/googlesamples/android-architecture), Activities
create Presenters
. Also Views
attach to Activity
and Presenters
get views (Fragments
) as parameter.
在提交Fragment
事务或恢复Fragment
(视图)状态后,创建Presenters
并以Fragments
(视图)为参数,而不是调用
After Fragment
transaction committed or Fragment
(view) state restored Presenters
get created and take Fragments
(views) as parameter than call
view.setPresenter(T presenter);
视图方法和Presenters
被注册以查看.
methods of views and Presenters
get registered to view.
我认为在Fragment
中创建Presenter
不是一个好习惯.首先,它们是单独的图层.对于关注点分离,这是非法的.其次,如果您在Fragment
中创建演示者,则将演示者的生活绑定到视图的LifeCycle
,并且当销毁并重新创建Fragment
时,您将创建一个新的演示者,但它们是不同的层.
I think creating Presenter
in Fragment
is not a good practice. First of all they are separate layers. This is illegal for Separation of concerns. And second, if you create presenter in Fragment
, you bind your Presenter's life to view's LifeCycle
and when Fragment
is destroyed and recreated, you create a new presenter but they're different layers.
模型是定义要在用户界面中显示或以其他方式作用的数据的界面.
The model is an interface defining the data to be displayed or otherwise acted upon in the user interface.
演示者对模型和视图起作用.它从存储库(模型)中检索数据,并将其格式化以在视图中显示.
The presenter acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.
视图是一个被动界面,用于显示数据(模型)并将用户命令(事件)路由到演示者以对该数据进行操作.
The view is a passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.
因此Activity
可以充当overall controller
来创建Presenters
和Views
并连接它们.
So Activity
can act as an overall controller
which creates the Presenters
and Views
and connect them.
如果我们谈论您的问题,是的,您可以分段注册演讲者.但是,您应该避免在用作视图的片段中创建演示者.
If we talk about your question, yes you can register presenter in fragment. But you should avoid creating presenters in fragments which you use as a view.
但是,关于Android社区中的MVP模式,有很多不同的方法,如下所示. https://plus.google.com/communities/114285790907815804707
But there're lot's of different approaches about MVP pattern in Android community like below. https://plus.google.com/communities/114285790907815804707
为什么活动不是ui元素? http://www.techyourchance.com/activities-android/
Why activities are not ui elements? http://www.techyourchance.com/activities-android/
这篇关于我可以在Fragment中注册MVP Presenter吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!