but from what I understand by using the above 2 lines, I am injecting MainActivity.class into the existing object graph
No. You are injecting dependencies into MainActivity. Your module is the dagger module and dependency at once :) First MySingle you create in Application onCreate method, second time you create MySingle during injection.
The @Module annotation should be on class that provides dependencies, and you should provide your dependencies there. Example:
@Module(
injects = {MainActivity.class,SecondActivity.class}
)
public class DependencyModule {
@Singleton
@Provides
public Repository getSingle(){
return new InMemoryRepository();
}
}
Some interface:
public interface Repository {
List<String> getAllStrings();
}
Its implementation:
public class InMemoryRepository implements Repository {
@Override
List<String> getAllStrings() {
// you can create this list as static list in constructor if you are not sure that it is really singleton :)
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
return list;
}
}
And your Activity:
public class MainActivity extends ActionBarActivity {
@Inject Repository myRepository;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyApplication app=(MyApplication)getApplication();
app.getObjectGraph().inject(this);
Log.v("testing", repository.getAllString().get(0));
}
}
You can have few implementations of repository: with database, in memory, in file, and you can only create its implementation and bind it to Repository interface via Module without touching the Activity code! With dagger you can inject into any class, not only Activity