Created
October 19, 2015 08:43
-
-
Save neilbantoc/30883b3ffa4f84b655e9 to your computer and use it in GitHub Desktop.
Dagger 2 Code Cheat Sheet (from https://guides.codepath.com/android/Dependency-Injection-with-Dagger-2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PerApp | |
@Component( | |
modules = { | |
SampleAppModule.class, | |
DataModule.class, | |
NetworkModule.class | |
} | |
dependencies = { | |
OtherComponent.class | |
} | |
) | |
public interface SampleAppComponent { | |
public void inject(SomeActivity someActivity); | |
public void inject(SomeFragment someFragment); | |
public void inject(SomeOtherFragment someOtherFragment); | |
Application application(); | |
Picasso picasso(); | |
Gson gson(); | |
OkHttpClient okHttpClient(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
public class SampleAppModule { | |
static final String PREFS_DEFAULT = "myapp"; | |
final SampleApp app; | |
public AppModule(SampleApp app) { | |
this.app = app; | |
} | |
@Provides @PerApp SampleApp provideSampleApp() { | |
return app; | |
} | |
@Provides @PerApp Application provideApplication(SampleApp app) { | |
return app; | |
} | |
@Provides @PerApp SharedPreferences provideSharedPrefs(Application app) { | |
return app.getSharedPreferences(PREFS_DEFAULT, Context.MODE_PRIVATE); | |
} | |
@Provides @PerApp Gson provideGson() { | |
return new Gson(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment