Architecture-samples: Why are presenters injected to activities and not to fragments?

Created on 8 Jun 2016  Â·  7Comments  Â·  Source: android/architecture-samples

Is there any benefit of injecting the presenters in Activities rather than in Fragments? The Activities do nothing but attach the Fragment which handles all the user interactions using the Presenter.

Take AddEditTaskActivity as an example. As the presenter is injected in it's onCreate() method, the presenter will be recreated during orientation changes. However if the presenter was injected into the AddEditTaskFragment it would have been retained through orientation changes too. This can be particularly useful in case the presenter has to maintain any state.

Just want to know what everyone thinks about this approach?

question

Most helpful comment

@JoseAlcerreca How can we use the same presenter on 2 fragments within the same activity? Don't you think we should have a sample of that since I think most devs use multiple fragments in the same activity.

All 7 comments

Yes that's also a valid approach. However, you need to know what you're doing because it's easy to abuse retaining fragments. We decided to show how to do a custom state save/restore mechanism.

I use it in Fragment. But I find Presenter will be NULL when I recycle my memory.

@JoseAlcerreca How can we use the same presenter on 2 fragments within the same activity? Don't you think we should have a sample of that since I think most devs use multiple fragments in the same activity.

@abhaysood it's risky to use setRetainInstance(true) in fragments, because it's too easy to have memory leaks. Here is explanation why http://stackoverflow.com/questions/13421945/retained-fragments-with-ui-and-memory-leaks/13422819. IMHO the best approach to MVP on Android is to treat activity as view and make it responsible for creating presenter and don't use fragments if you don't need to.

@samiuelson I don't think it's a good idea to advise against using fragments as a solution to the question. Sometimes fragments are very useful and even powerful, but the question still remains - how do fragments fit into MVP? We have two cases - one where we have a small fragment being created for each instance of something and another where we have something in the activity's layout file as well as a fragment being created in another space in that layout file. How should we use MPV in these instances?

but the question still remains - how do fragments fit into MVP?

@enkemari I wonder why this question is always shun... or when will it ever be given a proper answer..?

@pegashira i think fragments are the View in MVP since they implement the contract then listen for ui actions after which they pass them to the presenter through the contract i.e Contract.Presenter. On the other hand, activities instantiate and hold the fragment as well as instantiate or inject the concrete presenter class which the fragment will communicate to through the interface.

Was this page helpful?
0 / 5 - 0 ratings