简体   繁体   中英

How is the presenter created in MVP?

I was looking at the google samples for MVP and I saw this as the last statement in onCreate of the activity:

new TaskDetailPresenter(  
                taskId,  
                Injection.provideTasksRepository(getApplicationContext()),  
taskDetailFragment);

This code seems weird to me.
It instantiates an object ( TaskDetailPresenter ) that is local and not assigned anywhere and in it associates the fragment with the presenter.
Classes defined here
Is this really how it is supposed to be done? Because it seems not a good practice to me

In TaskDetailPresenter constructor they have:

mTaskDetailView.setPresenter(this);

So the presenter object is passed to the fragment ("View" in MVP). And that fragment stores a reference to the presenter. This is ok - View can easily communicate with its presenter with this configuration.

But the trick they use here - calling setPresenter(this) before the constructor finishes is a bad thing. Here you can find an explanation of this problem: http://www.javapractices.com/topic/TopicAction.do?Id=252

It may be ok if that reference is just saved to a property and everything happens in one thread. But something may change in the future and we can get into trouble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM