janishar / android-dagger2-example

This project implements the dagger 2 in android for dependency injection
455 stars 199 forks source link

[Question] why activityModule() is depreciated? #3

Closed sagar15795 closed 7 years ago

sagar15795 commented 7 years ago

In mainactivity.java

    public ActivityComponent getActivityComponent() {
        if (activityComponent == null) {
            activityComponent = DaggerActivityComponent.builder()
                    .activityModule(new ActivityModule(this))
                    .applicationComponent(DemoApplication.get(this).getComponent())
                    .build();
        }
        return activityComponent;
    }

I am getting depreciate error on line no-30 i.e. .activityModule(new ActivityModule(this)).

If you could explain me why is it happening then it will be helpful?

amitshekhariitbhu commented 7 years ago

@sagar15795 : Google has depreciated it. Check by clicking on that function.

sagar15795 commented 7 years ago

Yes, that I also know but I don't get the clear reason why is it depreciated and how to remove it? Sorry if it is a silly question because I am a beginner to Dagger 2.

amitshekhariitbhu commented 7 years ago

This is deprecated because new updated Dagger 2 can handle it automatically.

sagar15795 commented 7 years ago

So we don't need to add it now

janishar commented 7 years ago

@sagar15795 This deprecation is shown because Dagger2 has found that the ActivityModule do not provide any dependency and is redundant. The application will run the same way even if you remove the module ( i.e. remove this line: .activityModule(new ActivityModule(this))). If you add a provides method in the ActivityModule and inject that dependency somewhere then this deprecation will be removed because then it will be a requirement for DI.

sagar15795 commented 7 years ago

@janishar Thanks for the explanation.