Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

java - Can I just inject super class when use dagger2 for dependency injection?

I use Dagger2 for DI in my android application. I found that I have to write inject method for every class that uses @Inject field. Is there a way that I can just inject the parent class so that I don't have to call inject on every subclass? Take Activity for example. I have a BaseActivity that that every Activity extends from. Is there a way that I can just create an inject method in the component for BaseActivity and just call inject in BaseActivity's onCreate, and @inject fields in sub activities get injected automatically?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I encountered the same situation. One way to ease a bit the injection from a common component in all Activities is the following:

1) Extend the Application class to be able to create the common component and keep a reference to it.

public class ApplicationDagger extends Application {

    private ApplicationComponent component;

    @Override
    public void onCreate(){
        super.onCreate();
        component = DaggerApplicationComponent.builder().applicationModule(new ApplicationModule(this)).build();
    }

    public ApplicationComponent getComponent(){
            return component;
    }
}

2) Create an abstract DaggerActivity which gets the common component from Application and calls an abstract method injectActivity, giving the component as an argument. Like this:

public abstract class DaggerActivity extends Activity {

    @Override
    public void onCreate(Bundle saved){
        super.onCreate(saved);
        ApplicationComponent component = ((ApplicationDagger) getApplication()).getComponent();
        injectActivity(component);
    }

    public abstract void injectActivity(ApplicationComponent component);
}

3) Last, you have to actually inject each Activity extending DaggerActivity. But this can be done with less efforts now, as you have to implement the abstract method otherwise you'll get compile errors. Here we go:

public class FirstActivity extends DaggerActivity {

    @Inject
    ClassToInject object;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initialize your Activity
    }

    @Override
    public void injectActivity(ApplicationComponent component) {
        component.inject(this);
    }
}

Of course, you still have to declare each Activity explicitly in your Component.

UPDATE : Injecting @ActivityScope objects into Fragments

At some point, I needed to use custom scopes to bind objects to an Activity life cycle. I decided to extends this post as it might help some people.

Let's say you have a @Module class ActivityModule and a @Subcomponent interface ActivityComponent.

You would need to modify the DaggerActivity. The Activities extending DaggerActivity would need to implement the new method (change of signature).

public abstract class ActivityDagger extends AppCompatActivity {

    ActivityComponent component;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        component = ((ApplicationDagger) getApplication()).getComponent().plus(new ActivityModule(this));
        injectActivity(component);
        super.onCreate(savedInstanceState);
    }

    ActivityComponent getComponent() {
        return component;
    }

    public abstract void injectActivity(ActivityComponent component);
}

Then, a class FragmentDagger extending Fragment can be created like this :

public abstract class FragmentDagger extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityDagger activityDagger = (ActivityDagger) getActivity();
        ActivityComponent component = activityDagger.getComponent();
        injectFragment(component);
    }

    public abstract void injectFragment(ActivityComponent component);

}

As for the Activities, the Fragments extending FragmentDagger have only one method to implement:

public abstract void injectFragment(ActivityComponent component);

You should be able to reuse Fragments wherever you want. Notice that the method super.onCreated() in ActivityDagger should be called after the component instantiation. Otherwise, you will get NullPointerException when the Activity state is recreated, because the method super.onCreate() of the Fragment will be called.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...