1

I am using dagger 2 and retrofit I have Two Modules

1.Application Module

2.BlogModule

@Module
public class BlogModule {

   @PerActivity
    @Provides
    ApiService provideApiService(Retrofit retrofit){

        return retrofit.create(ApiService.class);
    }
}

ApplicationComponent

@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {

    Retrofit exposeRetrofit();
    Context exposeContext();
}

And I have added dependency in BlogComponent

@PerActivity
@Component(modules = BlogModule.class,dependencies = ApplicationComponent.class)
public interface BlogComponent {
    void inject(BaseActivity mainActivity);
}

And in My Application

public class BlogApplication extends Application {
    private ApplicationComponent mApplicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        initializeApplicationComponent();
    }


    private void initializeApplicationComponent() {

       mApplicationComponent= DaggerApplicationComponent
               .builder().applicationModule(new ApplicationModule(this))

                .build();
    }

    public ApplicationComponent getApplicationComponent(){

        return  mApplicationComponent;
    }

When I try to inject BlogComponent in My Base Actvity I cant able to add getApplicationComponent() in applicationComponent(getApplicationComponent())

DaggerBlogComponent.builder()
          .applicationComponent()
          .blogModule(new BlogModule())
          .build().inject(this);

As per tutorial they have injected as below

DaggerCakeComponent.builder()
        .applicationComponent(getApplicationComponent())
        .cakeModule(new CakeModule(this))
        .build().inject(this);

Can any one help me how I can fix this.thanks

Sagar
  • 23,903
  • 4
  • 62
  • 62
scott
  • 3,112
  • 19
  • 52
  • 90

1 Answers1

2

You should be able to access it from your application

DaggerBlogComponent.builder()
          .applicationComponent(((BlogApplication)getApplication()).getApplicationComponent())
          .blogModule(new BlogModule())
          .build().inject(this);
pradithya aria
  • 657
  • 5
  • 10
  • @pradithya.thanks for the answer.yes that we can do .i read below question answer ,as per him this is the bad practice so asked here .https://stackoverflow.com/questions/14998761/android-get-application-with-a-static-method – scott Apr 08 '18 at 14:34
  • I read that thread, the thing that is considered bad practice in that thread is to store application instance as static field and create a static method to access it. – pradithya aria Apr 08 '18 at 14:39
  • Found a better way https://medium.com/@iammert/new-android-injector-with-dagger-2-part-1-8baa60152abe – pradithya aria Apr 08 '18 at 14:42
  • 2
    @vision Ignore that other answer- there's nothing wrong with using a static method to get an application instance. Doing the same thing to get an Activity instance is a bad idea because it causes memory leaks, but an Application doesn't have the same problem (its always around anyway). Someone got confused. – Gabe Sechan Apr 08 '18 at 15:49
  • @GabeSechan.thank you so much for the comment.i am new be to android digging to learn dragger2 from last one month .each tutorial teaches different ways so.thank you for the comment – scott Apr 08 '18 at 15:52