0

I’m having an issue with dependency injection with Dagger, and using the MVP architecture, on a view which has dynamic tabs.

My activity is consisted basically of multiple tabs, which each tab has just a list of items (thus they all will be using the same Fragment class), and they all will use the same presenter class too (different instances). The number of tabs that I’ll be showing is based of a response I get from my server.

The problem is because dagger works on top of classes types, and I need to work on some sort of index to correlate the tab view and it's presenter.

I can't use the same instance of the presenter for all fragments because the presenter also keeps an reference to it's own fragment.

How can I achieve this kind of dynamic injection with dagger?

  • when creating a new fragment just use `setArguments(Bundle)` to pass a index in a `Bundle` to the fragment and use `getArguments()` in the fragment - just have a method on your presenter called `Presenter::setFragmentIndex(int index);` - set this on your `Presenter` instance in your `Fragment` after point of injection in the `Fragment` - same place you pass your View (Fragment) to your `Presenter`. – Mark Jan 26 '18 at 00:55
  • hey @MarkKeen thanks for the help. I don't think I can use the `Presenter::setFragmentIndex(int index)` method, because I still havent figured out how to create the multiple instances of the presenter. The presenter is also being instanciated on the module. – Lucas Cordeiro Jan 26 '18 at 01:22
  • one other thing I could try is creating a separate component and module containing only the fragment and presenter, and create a new component for each fragment I instanciate, but I don't think it's the best way to handle this. – Lucas Cordeiro Jan 26 '18 at 01:25
  • You scope your `Presenter` to your `Fragment` (The `Presenter` is added in your `Fragment` component/module) when you inject get an instance of your component. Or you can use unscoped. – Mark Jan 26 '18 at 10:53

0 Answers0