I have a module FragmentModule
@Module
public class FragmentModule
{
@Provides
public static PickerDashboardFragment providesPickerDashboard(int status,
Don't inject Fragments into your Activities using Dagger 2. Why? Fragments have a lifecycle controlled by the Android OS. When you add a Fragment to an Activity using a transaction, the FragmentManager will retain a reference to the Fragment. When the Activity instanceState
is saved, the Fragments added to FragmentManager will be saved. When the Activity is restored, if you request injection without checking for the presence of the Fragment in the FragmentManager, your Activity begin to reference two instances of the Fragment and create a memory leak.
For this reason, in void onCreate(Bundle savedInstanceState)
method you should check for the presence of the retained Fragment in the FragmentManager rather than request injection from Dagger 2. If the Fragment is not retained then you can instantiate it at that point. It is perfectly fine to use the new
keyword or static factories for this.
Example:
MyFragment frag;
void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.content);
frag = fragmentManager.findFragmentByTag(MyFragment.TAG);
if (frag == null) {
frag = MyFragment.instantiate(new Bundle());
}
}
However, at another level it seems you are asking how to combine parameters and dependencies. A good solution for these is often Factories. Say you have a CoffeeMaker:
class CoffeeMaker {
private final Kettle kettle;
private final Grinder grinder;
private final BeanFlavour beanFlavour;
CoffeeMaker(Kettle kettle, Grinder grinder, BeanFlavour beanFlavour) {
this.kettle = kettle;
this.grinder = grinder;
this.beanFlavour = beanFlavour;
}
}
The beanFlavour is variable (dark, roasted etc.) and varies and so is more like a parameter than a dependency. You could then write a CoffeeMakerFactory and inject this using Dagger 2:
class CoffeeMakerFactory {
private final Kettle kettle;
private final Grinder grinder;
@Inject
CoffeeMakerFactory(Kettle kettle, Grinder grinder) {
this.kettle = kettle;
this.grinder = grinder;
}
public CoffeeMaker create(BeanFlavour beanFlavour) {
return new CoffeeMaker(kettle, grinder, beanFlavour);
}
}
Factories are the standard solution for a combination of dependency and parameters see here and they can even be be generated using code generation tools like Google Auto.