Android Mvp - Can I Have Multiple Presenters For Custom Views And Fragments
Solution 1:
View (Activity) can have multiple Presenters. In case of having multiple CustomViews for Activity, you can have one giant Presenter or Presenter per each CustomView. It depends on this:
If all
CustomViewsshare same needs, onePresenterfor allCustomViewsis enough. Still two options forPresenter'sscope:Presenterhas ActivityScope.ActivityusesPresenterand gets called fromPresenter. Then sends commands, data toCustomViewsPresenterhas ViewScope. EachCustomViewcreates and destroys samePresenter
In case of
CustomViewsnot sharing same needs, having onePresenterandViewInterface, they will contain methods of allCustomViewsneeds, so eachCustomViewhas to implement all declared methods inViewInterface, leave some empty.If
CustomViewshave different needs and method calls toPresenter, they should have their ownPresenter.- If
CustomViewshave different needs and also some common needs, they share common need in onePresenter, specific needs in their ownPresenters. Example for this:ActivityOnehasCustomViewOneandCustomViewTwo. CommonPresenterfor bothCustomViewscan beFeedPresenter(considering both CustomViews have Feed List). ThenCustomViewOnewill haveCustomPresenter1andCustomViewTwowill haveCustomPresenter2for their specific needs.
Solution 2:
Best practice is to create a basepresenter , then create presenter for each view implementing basepresenter
Post a Comment for "Android Mvp - Can I Have Multiple Presenters For Custom Views And Fragments"