adennie / fb-android-dagger

A set of helper classes for using dagger 1 with Android components such as Applications, Activities, Fragments, BroadcastReceivers, and Services.
Apache License 2.0
282 stars 24 forks source link

InjectingActivity #15

Open fbcbl opened 9 years ago

fbcbl commented 9 years ago

Hey! First of all, loved the effort in making this lib. It sure helped me to understand Dagger a little better.

My question is:

Is there any way to extend your InjectingActivity without adding the specific classes to inject in the InjectingActivityModule? I would love to just extend your Activity and just run my app. Do I have to change the InjectingActivity to add something like this?

@Module(injects = {MyActivity.class})
adennie commented 9 years ago

You don't need to add classes to InjectingActivityModule -- for your project-specific injectable classes, you would add them to your own module classes, and in your InjectingActivity-derived classes, override getModules() and specify them:

public class MyActivity extends InjectingActivity {

@Inject MyInjectableThing thing;

...

@Override
protected List<Object> getModules() {
    List<Object> modules = super.getModules();
    modules.add(new MyModule());
    return modules;
}

}

@Module (injects={this.class, that.class, etc.}) public class MyModule { @Provides MyInjectableThing provideThing() { return new MyInjectableThing(); } }

When MyActivity.onCreate() calls super.onCreate(), InjectingActivity's onCreate() method will call getModules(), which will return a list of modules including MyModule. Then, onCreate will create a graph from that list of modules and inject itself, which will invoke the provideThing method and assign it's return value to MyActivity's thing field.

-Andy

Fábio Carballo wrote:

Hey! First of all, loved the effort in making this lib. It sure helped me to understand Dagger a little better.

My question is:

Is there any way to extend your InjectingActivity without adding the specific classes to inject in the InjectingActivityModule? I would love to just extend your Activity and just run my app. Do I have to change the InjectingActivity to add something like this?

@Module(injects = {MyActivity.class})

— Reply to this email directly or view it on GitHub https://github.com/adennie/fb-android-dagger/issues/15.

Fábio Carballo mailto:notifications@github.com Fri, February 13, 2015 2:01 PM

Hey! First of all, loved the effort in making this lib. It sure helped me to understand Dagger a little better.

My question is:

Is there any way to extend your InjectingActivity without adding the specific classes to inject in the InjectingActivityModule? I would love to just extend your Activity and just run my app. Do I have to change the InjectingActivity to add something like this?

@Module(injects = {MyActivity.class})

— Reply to this email directly or view it on GitHub https://github.com/adennie/fb-android-dagger/issues/15.

fbcbl commented 9 years ago

But what if I don't have a specific activity module? What if I just want to extend from InjectingActivity?

adennie commented 9 years ago

If your activity's injected dependencies can be satisfied by no-argument constructors or @Inject-annotated constructors, then you wouldn't need a module.

-Andy

Fábio Carballo wrote:

But what if I don't have a specific activity module? What if I just want to extend from InjectingActivity?

— Reply to this email directly or view it on GitHub https://github.com/adennie/fb-android-dagger/issues/15#issuecomment-74847736.