android MVP - can I have multiple presenters for custom views and fragments

后端 未结 2 1962
别跟我提以往
别跟我提以往 2021-01-02 04:56

So I have an a presenter that is already tied to an activity. The book says that one presenter should be tied to one view. But now I am adding a few fragments and lots of cu

相关标签:
2条回答
  • 2021-01-02 05:13

    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:

    1. If all CustomViews share same needs, one Presenter for all CustomViews is enough. Still two options for Presenter's scope:

      • Presenter has ActivityScope. Activity uses Presenter and gets called from Presenter. Then sends commands, data to CustomViews
      • Presenter has ViewScope. Each CustomView creates and destroys same Presenter

      In case of CustomViews not sharing same needs, having one Presenter and ViewInterface, they will contain methods of all CustomViews needs, so each CustomView has to implement all declared methods in ViewInterface, leave some empty.

    2. If CustomViews have different needs and method calls to Presenter, they should have their own Presenter.

    3. If CustomViews have different needs and also some common needs, they share common need in one Presenter, specific needs in their own Presenters. Example for this: ActivityOne has CustomViewOne and CustomViewTwo. Common Presenter for both CustomViews can be FeedPresenter(considering both CustomViews have Feed List). Then CustomViewOne will have CustomPresenter1 and CustomViewTwo will have CustomPresenter2 for their specific needs.
    0 讨论(0)
  • 2021-01-02 05:14

    Best practice is to create a basepresenter , then create presenter for each view implementing basepresenter

    0 讨论(0)
提交回复
热议问题