Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 how to perform constructor injection

I have a class

public class DialogUtils
{
    private Context context;

    @Inject
    public DialogUtils(Context context)
    {
        this.context = context;
    }
}

In my activity class i have did but i'm getting null pointer exception on dialogUtils instance.

public class LoginActivity extends Activity{
@Inject DialogUtils dialogUtils;
}

I know how to inject dependency via module and component but not sure how to with construction injection. Any help is much appreciated.

like image 409
eC Droid Avatar asked Jul 19 '17 14:07

eC Droid


People also ask

What is constructor injection in Dagger?

inject. Inject annotation to identify which constructors and fields it is interested in. Use @Inject to annotate the constructor that Dagger should use to create instances of a class. When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor.

What does @inject do Dagger?

Defining dependencies (object consumers) You use the @Inject annotation to define a dependency. If you annotate a constructor with @Inject , Dagger 2 can also use an instance of this object to fulfill dependencies. This was done to avoid the definition of lots of @Provides methods for these objects.

How do you inject an interface into a Dagger?

We can simply use @Inject at constructor! TL DR; If you have provide methods which just call constructor of implementation classes to inject interfaces, use @Binds annotation instead to get rid of boilerplate code in your dagger module.

How do you inject value at runtime in Dagger?

Inject values at runtime with UI in Dagger2:pureMathModule("Book Name") . build() . inject(this); The difference between DaggerComponent create() and in build() is - create() works when no runtime argument is passed into the constructor, else we use build() method.


2 Answers

If you don't retain the activity-level component and you aren't inheriting from a superscope (application-level component) using component dependency or subcomponent, then it's the following

// unscoped
public class DialogUtils {
    private final Context context;

    @Inject
    public DialogUtils(Context context) {
        this.context = context;
    }
}

then

@Module
public class ActivityModule {    
    private final Context context;

    public ActivityModule (Context context) {
        this.context = context;
    }

    @Provides //scope is not necessary for parameters stored within the module
    public Context context() {
        return context;
    }
}

@Component(modules={ActivityModule.class})
@Singleton
public interface ActivityComponent {
    Context context();

    DialogUtils dialogUtils();

    void inject(MainActivity mainActivity);
}

And then

@Inject
DialogUtils dialogUtils;

...


     ActivityComponent activityComponent = DaggerMainActivityComponent.builder()
        .activityModule(new ActivityModule(MainActivity.this))
        .build();

    activityComponent.inject(this); // activityComponent.dialogUtils() also works
like image 183
EpicPandaForce Avatar answered Oct 09 '22 10:10

EpicPandaForce


On the one hand you are registering DialogUtils for Constructor Injection, so any component could provide it.

On the other hand an activity and other parts of the Android Framework still need to be field injected. Dagger can't call their constructor, and @Inject DialogUtils dialogUtils; will not just magically appear.

Here you have to use a component, and register a method that takes your components type as an argument. Dagger will then create the method to inject your activities fields.

@Component MyComponent {
  inject(LoginActivity activity);
}

To inject the fields you still have to create your component, and call the inject(loginActivity) method.

void onCreate(...) {
  MyComponent component = // create the component

  // dagger does some heavy lifting here
  component.inject(this);

  dialogUtils.doSomething(); // not null, we just injected our fields
}
like image 32
David Medenjak Avatar answered Oct 09 '22 11:10

David Medenjak