3

I was working with Android MVP architecture and I am following Google MVP Architecture.

I am facing issue when Activity have multiple fragment,In my case activity having 2 tab. I am not able to understand, Should i create two Presenter for every Fragment OR should i create one Presenter for this? now same thing with View.

Even i did'nt found any solution on google-sample github repository.

Can anyone please suggest me or show me live code which accomplish my above requirement.

Ankit Saini
  • 374
  • 5
  • 15
  • If those two fragments are same then you can use same view and presenter. If fragments are bot same then different view and presenter will be better. – Shalauddin Ahamad Shuza Mar 27 '18 at 18:46
  • Thanx @ShalauddinAhamadShuza, I have one more query on this, How will activity interact with fragment, need to create separate Callbacks interface. would be better if Presenter can do this. like Google doing in single fragment example. – Ankit Saini Mar 27 '18 at 18:54
  • It depends on your project requirement and the use case – Shalauddin Ahamad Shuza Mar 29 '18 at 17:06

2 Answers2

0

If those two fragments are same then you can use same view and presenter. If fragments are bot same then different view and presenter will be better.

0

I always create different presenter/view for different fragments in an activity unless they share the same methods over some certain percentage. but to make thing more clean I believe creating things separately would be better.

1- You have to initialise your presenter inside onCreate() method of your Fragment. For this I always have a dependency registry class that is in charge of injection.

 public void inject(ReportingHistoryDetailsFragment fragment, ReportingHistoryResponse.TaskBean taskBean) {
        ReportingHistoryDetailsPresenter presenter = new ReportingHistoryDetailsPresenter(modelLayer,fragment,taskBean);
        fragment.configureWith(presenter);
    }

2- inside my fragment I call

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DependencyRegistry.shared.inject(this, taskBean);

    }

3- my Base view interface has

public interface BaseView <T> {

    void configureWith(T presenter);

}

if you have multiple fragments being managed by an activity, each fragment implements your View interface and have a corresponding presenter which implements your Presenter interface.

hope this helps.