dagger-2

Referencing the Activity inside its module

二次信任 提交于 2019-12-10 18:17:17
问题 How do I use the new AndroidInjector.inject and still be able to provide an Activity instance inside an Activity Module? The Dagger docs don`t make it clear how to archive this. The use case is the following: I have an Activity Module which provides a Presenter to my Activity, but the Presenter needs a reference to the Activity. I used to have something like @Inject Presenter presenter; public onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ((CustomApplication)

How to set and get Singleton object of a model class using dagger2?

泄露秘密 提交于 2019-12-10 14:32:18
问题 What options do I have to make a single instance of all the models classes in an Android application? I have added below one of the sample model classes public class User { private String email; private String name; public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } } I want that once data is stored in the Model Class, it can be retrieved

Dagger 2 : Cannot resolve symbol for dagger component

有些话、适合烂在心里 提交于 2019-12-10 13:43:34
问题 I would like to exercise this Dagger 2 Vehicle Motor example. I made everything exact like in that tutorial, except for my gradel.build: compile 'com.google.dagger:dagger:2.4' apt 'com.google.dagger:dagger-compiler:2.4' compile 'javax.annotation:javax.annotation-api:1.2' but then I get an error: cannot find symbol variable Dagger_VehicleComponent Whats wrong there? (Same without '_' underscore) 回答1: Another version solved it: compile 'com.google.dagger:dagger:2.2' apt 'com.google.dagger

Can not resolve symbol for dagger component in android

我是研究僧i 提交于 2019-12-10 13:38:16
问题 I'm trying to add Dagger2 to my project in Android Studio. This is my dependencies. compile 'com.google.dagger:dagger:2.8' annotationProcessor 'com.google.dagger:dagger-compiler:2.8' provided 'javax.annotation:jsr250-api:1.0' I am following this example. https://guides.codepath.com/android/Dependency-Injection-with-Dagger-2#advantages I am getting this error. Can not resolve symbol DaggerNetComponent I tried changing dependencies and other solution found after googling. but no luck. Can

Dagger 2 - how to create/provide a EagerSingleton

时间秒杀一切 提交于 2019-12-10 12:47:14
问题 I am having trouble with the Dagger 2 dependency injection framework. I would like to create an EagerSingleton . I assume that dagger 2 creates lazy loaded singletons when I use the @Singleton annotation. How do I create EagerSingleton using the Dagger 2 framework ? 回答1: I solved this by creating an EagerModule which had a single provide method which returned Void . Everything I wanted created eagerly I specified as parameters to that method. Then I added a Void init() method to the Component

Dagger multibinding of viewmodels using provides instead of constructor injection

大憨熊 提交于 2019-12-10 12:07:27
问题 All examples on how to combine ViewModel with dagger2 has been to use constructor injection on the ViewModelFactory. Like this: private final Map<Class<? extends ViewModel>, Provider<ViewModel>> providers; @Inject public ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> providers) { this.providers = providers; } While the module could look something like this. @Module abstract class ViewModelModule { @SuppressWarnings("unused") @Binds @IntoMap @ViewModelKey(value =

Multiple @Binds with same return class but different key

拥有回忆 提交于 2019-12-10 10:13:20
问题 All the following are used with dagger 2.10 and dagger.android package. Say I have a class I'd like to inject in multiple activities. public class DemoSharedClass { Activity activity; @Inject public DemoSharedClass(Activity activity) { this.activity = activity; } } Then, using the latest Dagger APIs, my classes are defined as public class DemoActivity extends DaggerActivity { @Inject DemoSharedClass demoSharedClass; // ... } public class Demo2Activity extends DaggerActivity { @Inject

How to get MainActivity inside a module using AndroidInjector

瘦欲@ 提交于 2019-12-10 10:04:42
问题 With dagger-android one now can simply write the following and successfully inject the app's dependencies: @Module public abstract class MainActivityModule { @ContributesAndroidInjector abstract MainActivity contributesMainActivity(); } @Singleton @Component(modules = { AndroidSupportInjectionModule.class, AndroidInjectionModule.class, AppModule.class, MainActivityModule.class }) public interface ApplicationComponent { void inject(BaseApplication baseApplication); @Component.Builder interface

AndroidInjector<android.app.Activity> cannot be provided without an @Provides- or @Produces-annotated method

删除回忆录丶 提交于 2019-12-10 04:35:34
问题 I'm getting the error while trying to get the new Android dagger classes from Dagger v2.11 working in our project. I'm not exactly sure what the problem is, as I've taken into account the Android documentation over at https://google.github.io/dagger//android.html and still cannot get over this issue. Any ideas on what's wrong with this setup? MyApplication: public class MyApplication extends Application implements HasActivityInjector { @Inject AndroidInjector<Activity> androidInjector;

Dagger2 cannot access nullable. javax.annotation.Nullable not found

家住魔仙堡 提交于 2019-12-10 04:28:55
问题 I have a module to provide a Retrofit interface. @Module class NetModule(val base: String) { @Provides @Singleton fun provideOkHttpClient(): OkHttpClient { return OkHttpClient.Builder() .addInterceptor(object: Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() info("Request for ${request.url()}") return chain.proceed(request) } }).build() } @Provides @Singleton fun provideGson(): Gson { return GsonBuilder()