2

As moving from beginner level coding and deeping into Android apps developing, patterns and architecture, to make my code cleaner and better. I met with a question, that I couldn't find a proper answer to.
And the question is: what is the proper way of using MVP pattern and Fragment? Should each layout have his own Activity, that will host Presenter and Fragment(as I heard putting Presenter to the Fragment itself is not the best practice), or it's better to have 1 hosting Activity that will host all Fragments and just replace them when needed, but create additional layer to manage Presenters switching? And things seem to become much harder when it comes to Tablet layouts or Tabs.
Google samples contains an example of using 1 Activity -> 1 Fragment, but I'm not sure if this is the best way, 'cause creating new Activity makes app 'blink'.

  • have look, [this](https://stackoverflow.com/a/42820930/5110595) might be helpful! – Hemant Parmar Feb 14 '18 at 07:24
  • @HemantParmar yes, I have seen that post, it's close to my question, but not exact, a guy in the answer tells that using Presenters inside of Fragments is not a good practice, so making something like Presenter switcher is the best way, but everything becomes harder when need to use Tablet layout or Tabs – Riabkov Stanislav Feb 14 '18 at 07:32

2 Answers2

3

In my apps I use multiple Activities, each with multiple Fragments, all implementing the following architecture. The communication between presenters is done by an event bus or local broadcasts. All presenters can be unit-tested with JUnit. Fragments and Activities do not contain any domain logic, just View manipulating stuff, not even that if using data binding.

enter image description here

artkoenig
  • 7,117
  • 2
  • 40
  • 61
0

Fragment also has the life cycle,we always see the fragment as the activity in the MVP model.Sometimes the dialog is the same.As long as it's the layout, we'll set it like this.This repositories below the google's mvp,activity,fragment..all represent V in the MVP model