Last active
July 12, 2016 11:06
-
-
Save goofyahead/3741f735b4b9a3f132ce60081b50687a to your computer and use it in GitHub Desktop.
Sample for dagger 2 in android
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
<?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> |
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
@Singleton | |
@Component( modules = {ApiModule.class}) | |
public interface ApiComponent { | |
void inject(MainActivity activity); | |
} |
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 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); | |
} | |
} |
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
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