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:
Inject in activity like this:
Fragment
Use this component in all your fragment:
Inject in fragment like this:
Application
Use this component in your Application:
Inject in Application like this: