Jim
Jim

Reputation: 4425

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

Upvotes: 0

Views: 282

Answers (1)

algrid
algrid

Reputation: 5964

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.

Upvotes: 2

Related Questions