The module that proides singletons of Gson, Retrofit and OkHttpClient
@Module
public class MyModule {
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
OkHttpClient client = new OkHttpClient();
return client;
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(BuildConfig.SERVER_BASE_URL)
.client(okHttpClient)
.build();
return retrofit;
}
}
The component that allows injecting the Singletons into activity and fragments
@Singleton
@Component(modules={MyModule.class})
public interface MyComponent {
void inject(Activity activity);
void inject(Fragment fragment);
void inject(Application application);
}
The main application class that builds the component
public class MyApp extends Application{
private MyComponent component;
@Inject
Retrofit retrofit;
@Override
public void onCreate() {
super.onCreate();
component= DaggerMyComponent.builder()
.myModule(new MyModule()).build();
getComponent().inject(this); // inject retrofit here
}
public MyComponent getComponent() {
return component;
}
}
This is the Fragment where I am trying to inject Retrofit.
public class MyFragment extends Fragment {
@Inject
Retrofit retrofit;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
((MyApp)getActivity().getApplication()).getComponent().inject(this);
....
}
}
In both MyApp and MyFragment the retrofit instance is null.
You can inject Activity,Fragment and Application in Same Component.You need to create Separate component for each Activity ,Fragment and Component Like this:
Activiy
Use this component in all your activity:
@Singleton
@Component(modules={MyModule.class})
public interface MyActivityComponent {
void inject(Activity activity);
void inject(AnotherActivity activity);
}
Inject in activity like this:
component= DaggerMyActivityComponent.builder()
.myModule(new MyModule()).build();
getComponent().inject(this)
Fragment
Use this component in all your fragment:
@Singleton
@Component(modules={MyModule.class})
public interface MyFragmentComponent {
void inject(Fragment fragment);
void inject(AnotherFragmen fragment);
}
Inject in fragment like this:
component= DaggerMyFragmentComponent.builder()
.myModule(new MyModule()).build();
getComponent().inject(this)
Application
Use this component in your Application:
@Singleton
@Component(modules={MyModule.class})
public interface MyAppComponent {
void inject(Application application);
}
Inject in Application like this:
component= DaggerMyAppComponent.builder()
.myModule(new MyModule()).build();
getComponent().inject(this)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With