preface

It is said on the Internet that Dagger2 is quite difficult to get started. I have read a lot of materials and encountered a lot of unknown or vague knowledge points when using Dagger2. Moreover, most of the blog materials are quite old. Suddenly for a moment, I realized why, so I summarized four articles. It’s still a bit cumbersome to use in Java, so don’t be afraid to get your hands dirty and apply it to our Android project.

This tutorial is divided into four parts: 1, Basic Knowledge of Dagger2 and using it in Java (1) 2, basic knowledge of Dagger2 and using it in Java (2) 3, Advanced knowledge of Dagger2 and using it in Android 4, Dagger2 is used in MVP framework

Using Dagger2 on Android in the previous article, I believe the code is pretty clean. And use more flexible. In this article, we will introduce how to use it in MVP programs. Presenters are added to the Activity in an annotation-dependent manner. And he’ll hold up a little chestnut. Please note that I’m going to talk about it from the perspective of the MVP Demo THAT I talked about earlier. Do not know the following link to see. Definitely benefit!


One caveat: I’m starting with my previous MVP post, which just happened to cover RxJava + Retrofit + MVP. This time we add Dagger2;

Read this article if you don’t knowRxJava + Retrofit + MVP For beginners,VIP MVP frame!!


I’ll talk about it from the perspective of the MVP in the previous article.

First add the Dagger2 dependency

    // Bring in the dagger. Android library
    implementation 'com. Google. Dagger: a dagger - android: 2.24'
    // if you use the support libraries
    implementation 'com. Google. Dagger: a dagger - android - support: 2.24'
    annotationProcessor 'com. Google. Dagger: a dagger - compiler: 2.24'
    annotationProcessor 'com. Google. Dagger: a dagger - android - processor: 2.24'
Copy the code

Then create a daggerforandroid package in the MVP project, add our AppComponent and the registered Module: NeedInjectModules. Here we just change the POSTFragment to a Fragment that uses Dagger2

NeedInjectModules are as follows, and I won’t describe what these annotations do here. That was pretty clear in the last post

@Module
public abstract class NeedInjectModules {
    @ContributesAndroidInjector
    abstract POSTFragment injectPOSTFragment(a);
}
Copy the code

AppComponent as follows,

@Component(modules = {
        AndroidSupportInjectionModule.class,
        NeedInjectModules.class,
})

public interface AppComponent extends AndroidInjector<MyApplication> {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);
        AppComponent build(a); }}Copy the code

To write this, the data we need to initialize is Presenter. Since I have no parameter, I use @inject directly. If you need parameters, use Module. After you finish this, please remember to Make Project.

public class PostPresenter extends BasePresenter<PostContract.View> implements PostContract.Prensenter {

    @Inject
    public PostPresenter(a){}...// Omit some code for easy understanding
}
Copy the code

Modify our Application as follows, inherit DaggerApplication, the DaggerAppComponent. Builder back out

public class MyApplication extends DaggerApplication {
    
    @Override
    public void onCreate(a) {
        super.onCreate();
    }

    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        return DaggerAppComponent.builder().application(this).build(); }}Copy the code

The key to the

Earlier we used RxJava + Retrofit + MVP Demo. To solve RxJava memory leaks, we used RxActivity and RxFragment.

Using Dagger2 on Android, you need to inherit from DaggerActivity and DaggerFragment.

What to do at this point. Let’s look at the source of our DaggerActivity;

Also note that we annotate mPresenter with @Inject and remove cretaePresenter from our previous page new method. done

public abstract class BaseDaggerActivity<T extends BasePresenter> extends RxFragmentActivity implements BaseView.HasAndroidInjector {
    @Inject
    public T mPresenter;

    //public abstract T cretaePresenter();.// Omit some code for easy understanding
}
Copy the code

And that’s it. BaseDaggerFragment does the same thing. If you need an annotation, you just inherit from BaseDaggerActivity, if you don’t need an annotation, you just inherit from BaseActivity.

The above is using Dagger2 dependency injection for Presenter.

Here’s a little example that will keep you hooked on the Dagger2

After the above operation is clear, I used dependency injection in the POSTFragment in Demo, and the code is as follows:

public class POSTFragment extends BaseDaggerFragment<PostPresenter> implements PostContract.View {

    @Inject
    Woman woman;
    
    @OnClick(R.id.btn_dagger)
    public void daggerClick(a) {
        ToastUtils.showToast(woman.getSoul().getMoney() + ""); }}Copy the code

Toast = 100;

Let’s look at the Woman’s code and inject Soul

public class Woman {
    @Inject
    Soul soul;

    @Inject
    public Woman(a) {}public Soul getSoul(a) {
        return soul;
    }

    public void setSoul(Soul soul) {
        this.soul = soul; }}Copy the code

Let’s look at the code for Soul, and of course you can use Module to pass values.

public class Soul {
    private int money = 100;
    @Inject
    public Soul(a) {}public int getMoney(a) {
        return money;
    }

    public void setMoney(int money) {
        this.money = money; }}Copy the code

So finish the Woman class and the Soul class. You don’t have to do anything else. Of course, if you pass a Module value, write it in the AppComponent!! Is it convenient? It’s convenient.

Conclusion: At this point, all our four articles are over. Dagger2 works well on Android. It saves all the new, if you use a class new for 100 more pages. But suddenly high demand, change constructor or something, do you just need to change it in Module? Perfect decoupling

This article github Demo address

A lot of work. Don’t get me wrong, not for money. Can you leave a footprint star