Skip to content

Instantly share code, notes, and snippets.

@goofyahead
Last active July 12, 2016 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goofyahead/3741f735b4b9a3f132ce60081b50687a to your computer and use it in GitHub Desktop.
Save goofyahead/3741f735b4b9a3f132ce60081b50687a to your computer and use it in GitHub Desktop.
Sample for dagger 2 in android
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="socialtech.com.triboo"
>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".base.YourApplication"
>
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
@Singleton
@Component( modules = {ApiModule.class})
public interface ApiComponent {
void inject(MainActivity activity);
}
@Module
public class ApiModule {
private final Context mContext;
public ApiModule(Context context) {
this.mContext = context;
}
@Provides
@Singleton
RequestQueue provideQueue() {
return Volley.newRequestQueue(mContext);
}
@Provides
@Singleton
ImageLoader provideImageLoader(RequestQueue mRequestQueue) {
return new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> cache = new LruCache<>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
@Provides
@Singleton
YourApiInterface provideApi() {
return new Retrofit.Builder().baseUrl(mContext.getString(R.string.apiEndPoint)).build().create(YourApiInterface.class);
}
}
public class YourAplication extends Application {
private ApiComponent apiComponent;
@Override
public void onCreate() {
super.onCreate();
// Dagger%COMPONENT_NAME%
apiComponent = DaggerApiComponent.builder()
.apiModule(new ApiModule(this))
.build();
}
public ApiComponent getApiComponent() {
return apiComponent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment