Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting on adapters with dagger in android

I'm just trying dagger instead of roboguice, so far butterknife was awesome and simple, point for it :)

But dagger on the other hand I found it less configurable than roboguice, I have to benchmark if its worth the change but in this case I'm looking on how to inject stuff in lets say Adapters, this is what I made and it works:

public class PeopleAdapter extends BaseAdapter {

private static final String TAG = PeopleAdapter.class.getName();
@Inject
TempoSharedPreferences prefs;

private LinkedList<People> elements;

public PeopleAdapter (LinkedList<People> elements, TempoApplication app) {
    this.elements = elements;
    app.inject(this);
    Log.d(TAG, "registered: " + prefs.isRegistered());
} ....

But on the Activity that creates this instance I have to get an Application that allows to inject, also I have to add to the module every time the classes that use that dependency, roboguice did all that for me and had only one entry point where to modify the stuff.

I'm I doing something wrong? is there any better way to perform this injections? Avoid the declaration of each class on the module?

@Module(injects = {
    MainActivity.class,
    PeopleAdapter.class
    },
    library = true)
public class AndroidModule { ....

I'll appreciate any comment or best practice or experience on this.

Thanks!

like image 294
Goofyahead Avatar asked Aug 03 '14 17:08

Goofyahead


People also ask

What is Dagger dependency injection Android?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.

What does inject mean in Dagger?

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.

What is @inject in Android?

You pass the dependencies of a class to its constructor. Field Injection (or Setter Injection). Certain Android framework classes such as activities and fragments are instantiated by the system, so constructor injection is not possible. With field injection, dependencies are instantiated after the class is created.

Can a Dagger inject a private field?

Dagger cannot support private fields and still support code-generated adapters (to avoid reflection). The way systems like Guice support private fields is they change the access to the field reflectively before accessing them.


1 Answers

You're doing everything right. Dagger needs some more configuration than Roboguice, but is also more powerful (configurable). Did you try scoped graphs or lazy injection already?

Every single class that uses injection must be listed in the injects param of the module. There's no way to avoid declaration of each class.

like image 148
tomrozb Avatar answered Oct 22 '22 02:10

tomrozb