I am attempting to use Dagger as my Android app's dependency injection library. In my project, I have different Android modules in the project representing different flavors of the app. I want to use dependency injection to allow each module to define its own navigation menu.
My MenuFragment class requires an instance of my interface (MenuAdapterGenerator):
public class MenuFragment extends Fragment {
@Inject
protected MenuAdapterGenerator generator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//this.generator is always null here, though shouldn't it be injected already?:
BaseExpandableListAdapter adapter = new MenuAdapter(inflater, this.generator);
}
}
This is what my menu module looks like:
@Module (
injects = MenuAdapterGenerator.class
)
public class MenuDaggerModule {
public MenuDaggerModule() {
System.out.println("test");
}
@Provides @Singleton MenuAdapterGenerator provideMenuAdapterGenerator() {
return new MenuNavAdapterGenerator();
}
}
Here is the overall app-level module (which includes this MenuDaggerModule):
@Module (
includes = MenuDaggerModule.class,
complete = true
)
public class OverallAppModule {
}
(Edit:) Here is my MainActivity class which creates the object graph:
public class MainActivity extends Activity {
private ObjectGraph objGraph;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.objGraph = ObjectGraph.create(OverallAppModule.class);
this.mainWrapper = new MainWrapper(this, this.objGraph);
this.setContentView(R.layout.activity_main);
//Other instantiation logic
}
(Edit:) And here is where I actually make my MenuFragment (in MainWrapper):
public class MainWrapper {
public MainWrapper(Activity activity, ObjectGraph objGraph) {
this.menu = new MenuFragment();
this.objGraph.inject(this.menu);
//I have no idea what the above line really does
FragmentManager fm = this.activity.getFragmentManager();
FragmentTransaction t = fm.beginTransaction();
t.replace(R.id.menu_fragment, this.menu);
t.commit();
}
}
Why is my module's provideMenuAdapterGenerator method not called to inject my MenuAdapterGenerator? If I set a breakpoint in that method, it is never tripped. But the MenuDaggerModule is getting created, because System.out.println("test"); is being hit.
My understanding is that if the MenuDaggerModule is created (which it is), Dagger should then use that provideMenuAdapterGenerator() anytime a @Injects MenuAdapterGenerator is encountered. What do I have wrong?