Reputation: 681
I want to ask how to do the injection of a constructor that doesn't have any parameters. I have come across this problem but unable to find a solution. Here is my repo class
public class UrlsRepository {
private UrlsSource urlsSource;
@Inject
public UrlsRepository(UrlsSource urlsSource){
this.urlsSource = urlsSource;
}
public String getLandingUrl(){
return urlsSource.getLoginUrl();
}
public String[] getDashboardUrls(){
return urlsSource.getDashboardUrls();
}
}
Here is the interface
public interface UrlsSource{
String getLoginUrl();
String[] getDashboardUrls();
}
and below is the implementation
@Singleton
public class LocalUrlsSource implements UrlsSource {
@Inject
public LocalUrlsSource(){}
@Override
public String getLoginUrl() {
return Properties.LOGIN_URL;
}
@Override
public String[] getDashboardUrls() {
return Properties.DASHBOARD_URLS;
}
}
Now inside my main activity, I'm injecting UrlsRepository but it's null. Please guide.
public class MainActivity extends Activity implements MainPresenter.View {
@Inject
UrlsRepository urlsRepository;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Upvotes: 1
Views: 309
Reputation: 824
If you wan't to inject interfaces like this you need to create a @Module with a @Binds annotated method like this.
@Module
abstract class UrlModule {
@Binds
abstract UrlsSource bindUrlsSource(LocalUrlsSource source);
}
And then you would create a component which uses this module and inject into your activity.
@Component(modules = {UrlModule.class})
@Singleton
public interface AppComponent {
void inject(MainActivity activity);
}
Then initiate the component in your Application.java class and retrieve it inside your activity and call inject(), for example like this.
public final class MainApplication extends Application {
private static AppComponent appComponent;
public static AppComponent getAppComponent() {
return appComponent;
}
private static AppComponent createAppComponent(Application application) {
return DaggerAppComponent.create();
}
@Override
public void onCreate() {
super.onCreate();
appComponent = createAppComponent(this);
}
}
Then you would inject the dependency like this.
public class MainActivity extends Activity implements MainPresenter.View {
@Inject
UrlsRepository urlsRepository;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Injects this activity.
MainApplication.getAppComponent().inject(this);
}
}
Upvotes: 1