Skip to content

Instantly share code, notes, and snippets.

@jvergeldedios
Created September 19, 2014 05:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jvergeldedios/a5b4b83f574e330c51cf to your computer and use it in GitHub Desktop.
Simple example of Dagger in Android
public class MyApp extends Application {
private ObjectGraph objectGraph;
public void onCreate() {
super.onCreate();
objectGraph = ObjectGraph.create(new AppModule(this));
}
public static void inject(Context context, Object toInject) {
MyApp app = (MyApp)context.getApplicationContext();
app.objectGraph.inject(toInject);
}
public static void inject(Context context) {
inject(context, context);
}
}
@Module(
injects = {
FooActivity.class
}
)
public class AppModule {
private Context context;
public AppModule(Context context) {
this.context = context;
}
@Singleton @Provides
GSHTTPClient provideGSHTTPClient() {
return new GSHTTPClient(context);
}
}
public class FooActivity extends Activity {
@Inject
GSHTTPLib httpLib;
public void onCreate() {
super.onCreate();
MyApp.inject(this)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment