Search code examples
androiddagger-2dagger

Android Dagger-2 how to provide the dependency for method parameters


I have a module FragmentModule

@Module
public class FragmentModule
{
    @Provides
    public static PickerDashboardFragment providesPickerDashboard(int status, String name, Object someComplexObject)
    {
        PickerDashboardFragment fragment = new PickerDashboardFragment();
        Bundle b = new Bundle();
        b.putInt("status", status);
        b.putString("name", name);
        b.putInt("object", someComplexObject);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Provides
    public static PickingFragment providesPickingFragment()
    {
        PickingFragment fragment = new PickingFragment();
        return fragment;
    }
}

Here's my Component class

@Component(modules = {UtilModule.class, FragmentModule.class})
public interface ApplicationComponent
{
    void inject(PickerDashboardActivity target);
}

In my activity this is how i'm injecting the PickerDashboardActivity

@Inject 
PickerDashboardFragment frag;

ApplicationComponent component = DaggerApplicationComponent.builder().build();
        component.inject(this);

My question is what's the best and easiest way to provide the dependencies for PickerDashboardFragment providesPickerDashboard(int status, String name, Object someComplexObject) i.e status, name and someComplexObject.

Best Regards


Solution

  • Add attributes and Provides methods to your module like this:

    @Module
    public class FragmentModule
    {
    
        private final int status;
        private final String name;
        private final Object someComplexObject;
    
        public FragmentModule(int status, String name, Object someComplexObject) {
            this.status = status;
            this.name = name;
            this.someComplexObject = someComplexObject;
        }
    
        @Provides
        int providesStatus() {
            return status;
        }
    
        @Provides
        String providesName() {
            return name;
        }
    
        @Provides
        Object providesSomeComplexObject() {
            return someComplexObjext;
        }
    
        @Provides
        public static PickerDashboardFragment providesPickerDashboard(int status, String name, Object someComplexObject)
        {
            PickerDashboardFragment fragment = new PickerDashboardFragment();
            Bundle b = new Bundle();
            b.putInt("status", status);
            b.putString("name", name);
            b.putInt("object", someComplexObject);
            fragment.setArguments(bundle);
            return fragment;
        }
    
        @Provides
        public static PickingFragment providesPickingFragment()
        {
            PickingFragment fragment = new PickingFragment();
            return fragment;
        }
    }
    

    Having a module providing ints and Strings will probably make you use some qualifiers (such as Named) in order to avoid collisions