dagger-2

Dagger2 component with more than one dependencies

依然范特西╮ 提交于 2019-11-30 01:51:32
问题 This is what I currently have and it works: @FragmentScope @Component(dependencies = {FacebookComponent.class}, modules = {FragmentFacebookLoginModule.class}) public interface FragmentFacebookLoginComponent { void inject(FragmentFacebookLogin fragment); } Now I want to add another dependency. I changed it to this: @Component(dependencies = {FacebookComponent.class, AnotherComponent.class}, modules = {FragmentFacebookLoginModule.class}) But now I get this error message:

Context cannot be provided without an @Provides-annotated method, but it is?

佐手、 提交于 2019-11-30 01:34:32
问题 I have the following simple module: @Module public class ApplicationModule { private CustomApplication customApplication; public ApplicationModule(CustomApplication customApplication) { this.customApplication = customApplication; } @Provides @Singleton CustomApplication provideCustomApplication() { return this.customApplication; } @Provides @Singleton @ForApplication Context provideApplicationContext() { return this.customApplication; } } And the respective simple component: @Singleton

Dagger 2 injecting multiple instances of same object type

谁都会走 提交于 2019-11-30 00:53:48
问题 Background I am converting my app to MVP architecture and found Dagger 2 to be useful to inject dependencies when needed. My app needs to communicate with two web apis (my own and a third party api). There may be times where requests to my own api and the third party api could fire at the same time. I am using Retrofit to communicate with these apis and using GSON for serialistation / deserialisation. What I did before I created two Retrofit RestAdapters and used Service Locator pattern to

Using Dagger 2 to inject into service

北慕城南 提交于 2019-11-30 00:43:50
I have an app which is basically a service that runs all the time and alarms the user when something happens. When the service creates the alarm, it needs to give it his context so that the alarm can do callbacks to the service when something happens. For example: public MyService extends Service{ private SomeAlarm alarm; @Override public void onCreate() { super.onCreate(); alarm = new SomeAlarm(MyService.this); } } How can I inject the SomeAlarm class into the service, and give the SomeAlarm the service context as a variable? I wrote the code from the top of my head, so there could be a typo

Dagger 2 lifecycle of a component, module and scope

二次信任 提交于 2019-11-29 20:36:52
I've read a lot of posts and tutorials about dagger 2: http://frogermcs.github.io/dependency-injection-with-dagger-2-custom-scopes/ https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2 http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android/ https://github.com/konmik/konmik.github.io/wiki/Snorkeling-with-Dagger-2 What determines the lifecycle of a component (object graph) in Dagger 2? etc. But I am still confused about the lifecycle of a component, and how it relates to module and scope. I want to make sure I don't create multiple instances of an object

Dagger + Retrofit. Adding auth headers at runtime

不羁岁月 提交于 2019-11-29 20:28:09
I'm wondering if there is a way for Dagger to know that it should recreate an object when new data is available. The instance I am speaking of is with the request headers I have for retrofit. At some point (when the user logs in) I get a token that I need to add to the headers of retrofit to make authenticated requests. The issue is, I'm left with the same unauthenticated version of retrofit. Here's my injection code: @Provides @Singleton OkHttpClient provideOkHttpClient(Cache cache) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel

Dagger 2 Injecting Constructors

﹥>﹥吖頭↗ 提交于 2019-11-29 20:27:16
I'm starting to use Dagger 2 in an application I'm developing but I have some questions about how Dagger 2 works. I get the all the logic behind the @Provides methods and the @Inject annotation for initialising your dependencies, but the @Inject annotation to class constructors kind of bugs my mind. For example: Im my app, I have one module defined, the ContextModule, to retrieve the context of my application: ContextModule.java @Module public class ContextModule { private final Context context; public ContextModule(Context context) { this.context = context; } @Provides public Context context(

Dagger2 dependency - Gradle

痞子三分冷 提交于 2019-11-29 20:25:01
I'm trying to add Dagger2 to my project in Android Studio but I can't find proper dependency to paste in build.gradle. Could you help and send me the proper line? Installing Dagger 2 on Android Studio 2 // Application build.gradle dependencies { compile 'com.google.dagger:dagger:2.4' annotationProcessor "com.google.dagger:dagger-compiler:2.4" } Maven Repositories: Find the latest versions of the above dependencies in the Maven Repository: dagger dagger-compiler Notes: Android Studio < 2.2 Older versions of Android Studio need android-apt for annotation processing. // Project build.gradle

Dagger 2 examples [closed]

扶醉桌前 提交于 2019-11-29 19:49:15
Dagger 2 is around the corner but the available examples wouldn't even compile right off the box, and the documentation is a copy-paste-replace from Dagger 1. Does anyone have an example of a proper application working on Google's Dagger 2? I've just published sample app based on Gradle which integrates Dagger2, retrolambda, butterknife and lombok. You can find it here: https://github.com/mgrzechocinski/dagger2-example . Hope it would help :) I've just ported u2020-mvp app to Dagger 2. We use it as our sandbox app. Dagger 2 has nice implementation of scopes by the way. Components are really

Dagger 2 with MVP, avoid creating extra presenter object on view recreation

泄露秘密 提交于 2019-11-29 16:29:15
问题 I have an app implementing the MVP pattern with a Loader to maintain the presenter object on view recreation (there's an article about this here). I'm new to Dagger 2, trying to implement it together with the current code. I've managed to make it work, but now my presenter is created twice. At first it was created using a factory class which was initialized in onCreateLoader , but then, when adding Dagger 2 implementation, I had two objects created (in factory class and when injecting). Now I