Dagger2 of google four-piece suit. From entry to love, it: Dagger2 gorgeously used in the MVP framework

Preface

It is said on the Internet that Dagger2 is more difficult to get started. I have encountered many unintelligible or vague knowledge points when I read a lot of materials and used it, and most of the blog materials are relatively old. Suddenly for a moment, I suddenly understood why, so I summarized 4 articles. It is still very cumbersome to use in java. Don't be afraid to take you to get started and apply it to our Android project.

This Dagger2 explanation is divided into 4 articles:
1. Basic knowledge of Dagger2 and its use in Java (1)
2. Basic knowledge of Dagger2 and its use in Java (2)
3. Advanced knowledge of Dagger2 and use in Android
4. Dagger2 gorgeous Used in MVP framework

Through the use of Dagger2 in Android, I believe the code is concise enough. And it is more flexible to use. In this article, we will introduce how to use it in MVP projects. The previous Presenter will be added to the Activity in the form of dependent annotations. And will give a little chestnut. Please note that I will explain from the perspective of the MVP Demo I mentioned earlier. If you don’t understand, you can go to the link below. Definitely benefited a lot!


Special reminder: I'm here to explain with my previous MVP, which happened to only involve RxJava + Retrofit + MVP. This time we add Dagger2;

If you don’t understand, go to read this article RxJava + Retrofit + MVP (I don’t understand after reading it, complain about me. Suitable for beginners, VIP version MVP framework!!)


I will talk about it from the perspective of the previous MVP.

First add Dagger2 dependency

    //引入dagger.android库
    implementation 'com.google.dagger:dagger-android:2.24'
    // if you use the support libraries
    implementation 'com.google.dagger:dagger-android-support:2.24'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'


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


NeedInjectModules are as follows , I will not describe the role of these annotations here. The last article is very clear

@Module
public abstract class NeedInjectModules {
    
    
    @ContributesAndroidInjector
    abstract POSTFragment injectPOSTFragment();
}


AppComponent is 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();
    }

}


At this point, the data we need to initialize is Presenter. , Because I have no parameters, I use @Inject directly. If you need to take parameters, please use Module. After finishing this, please remember to Make Project.

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

    @Inject
    public PostPresenter(){
    
    

    }
    ...//省略部分代码,便于理解
}


Modify our Application as follows , inherit DaggerApplication, and return DaggerAppComponent.Builder

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

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

}

Here comes the point

Before we used RxJava + Retrofit + MVP Demo, in order to solve the memory leak of RxJava, we used RxActivity and RxFragment.


Through the previous article Dagger2 used in Android, it is necessary to inherit DaggerActivity and DaggerFragment. .

What to do at this time. Let's take a look at the source code of DaggerActivity;


After reading it, do you feel it? Let's create a new BaseDaggerActivity code that is exactly the same as the previous BaseActivity. The only difference is to implement the implements HasAndroidInjector interface and configure the parameters according to the source code.

At the same time, pay attention to adding @Inject annotation to mPresenter, and then delete the new method cretaePresenter on our previous page. nailed it

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

    //public abstract T cretaePresenter();
    
    ...//省略部分代码,便于理解
}


After doing the above, you are done. The same is true for BaseDaggerFragment. If you need annotations, you only need to inherit BaseDaggerActivity. If you don't need annotations, you only need to inherit BaseActivity .

The above is the use of Dagger2 dependency injection Presenter.

Here is a small example that will make you love Dagger2

After the above operations are clear, I used dependency injection in the POSTFragment in the Demo. 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() {
    
    
        ToastUtils.showToast(woman.getSoul().getMoney() + "");
    }
    
}

The toast result is: 100;


Let's take a look at Woman's code and inject Soul

public class Woman {
    
    
    @Inject
    Soul soul;

    @Inject
    public Woman() {
    
    

    }

    public Soul getSoul() {
    
    
        return soul;
    }

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


Let's take a look at Soul's code. Of course, you can use Module to pass values.

public class Soul {
    
    
    private int money = 100;
    @Inject
    public Soul() {
    
    

    }

    public int getMoney() {
    
    
        return money;
    }

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

Finish writing the Woman class and Soul class like this. It can be used without any other operations. Of course, if you use Module to pass a value, you must write it in AppComponent! ! Is it very convenient ?

Concluding remarks: So far, our 4 articles are all over. After reading it, do you think Dagger2 is easy to use in Android? Save all the new process, if you use the new class for 100 pages. But suddenly there is a high demand, and the construction method is changed, or some other methods, is it only necessary to modify it in the Module? Perfect decoupling

This article github Demo address

Don't get me wrong, it's not asking for money. Can you leave a footprint star?

Guess you like

Origin blog.csdn.net/leol_2/article/details/100560598