erluxman erluxman - 1 year ago 135
Android Question

Dagger 2 does not inject my Object but can be obtained from component

I have my component like

@Component(modules = { GithubListActivityModule.class,GlideActivityModule.class })
public interface GithubListActivityComponent {

GithubUserListAdapter githubUserListAdapter ( );
RequestManager requestManager();
LinearLayoutManager linearLayoutManager();


And I have a module like this :

public class GithubListActivityModule {
private final Activity githubListActivity;

public GithubListActivityModule ( Activity activity ) {
this.githubListActivity = activity;


Activity activity ( ) {
return this.githubListActivity;

public LinearLayoutManager linearLayoutManager(Activity activity){
return new LinearLayoutManager ( activity );

Problem :
I have treid to inject LinearLayout manager like this :

LinearLayoutManager linearLayoutManager;

While my Component is built like this :

githubListActivityComponent = DaggerGithubListActivityComponent.builder ()
.githubListActivityModule ( new GithubListActivityModule ( this ) )
.build ();

my Linear Layout manager does not get instantiated. But when I manually do

linearLayoutManager = githubListActivityComponent.linearLayoutManager ();

It works fine. Where am I going wrong?

Answer Source

Everywhere that I am passing Activity , I should pass exactly the same class Name (not its parent) So once edited every parameters and return types that were "Activity" into "GithubListActivity" and then added

void inject( GithubListActivity activity);

inside the component class

then injected "GithubListActivity" like this :

DaggerGithubListActivityComponent.builder ()
            .githubListActivityModule ( new GithubListActivityModule ( this ) )
            .build ().inject ( this );

Then the codes worked for me ..

Lesson :
1. Define inject method and Inject the current Activity

2. Use exactly the same type of object (not the parent ) in this case use "GithubListActivity" instead of just activity.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download