网上都说Dagger2是比较难上手的,我在看了大量资料和使用时也遇到了不少不懂或者模糊的知识点,并且大部分博客资料都比较古老。忽然有那么一瞬间,忽然明白了因此然,故总结了4篇文章。话说在java中使用仍是很繁琐的,不要怕带你真正上手,并运用到咱们的Android项目中去。java
本次Dagger2讲解总共分4篇:
一、Dagger2基础知识及在Java中使用(1)
二、Dagger2基础知识及在Java中使用(2)
三、Dagger2进阶知识及在Android中使用
四、Dagger2华丽使用在MVP框架中android
经过上一篇Dagger2在Android中的使用,我相信代码已经够简洁。并且使用比较灵活了。这篇咱们将介绍,怎么在MVP项目里用。会将以前Presenter以依赖注解的方式到Activity里。并且会举个小栗子。请注意这里我会以我以前讲的那篇MVP Demo的角度讲解。不了解的能够经过如下连接去看。绝对受益不浅!git
首先添加上Dagger2依赖github
//引入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'
复制代码
而后在MVP项目里新建daggerforandroid包,加上咱们的AppComponent 以及注册的Module: NeedInjectModules。这里咱们只把POSTFragment改为使用Dagger2的Fragmentapp
NeedInjectModules以下,我这里再也不叙述这些标注的做用。上一篇都说的很清楚了框架
@Module
public abstract class NeedInjectModules {
@ContributesAndroidInjector
abstract POSTFragment injectPOSTFragment();
}
复制代码
AppComponent以下,ide
@Component(modules = {
AndroidSupportInjectionModule.class,
NeedInjectModules.class,
})
public interface AppComponent extends AndroidInjector<MyApplication> {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
}
复制代码
写到这里,咱们须要初始化的数据是Presenter。,由于我是无参,因此直接用@Inject。若是你须要带参数,请注意使用Module。作完这部后,请记得Make Project.post
public class PostPresenter extends BasePresenter<PostContract.View> implements PostContract.Prensenter {
@Inject
public PostPresenter(){
}
...//省略部分代码,便于理解
}
复制代码
修改咱们的Application以下,继承DaggerApplication,把DaggerAppComponent.Builder返回出去ui
public class MyApplication extends DaggerApplication {
@Override
public void onCreate() {
super.onCreate();
}
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().application(this).build();
}
}
复制代码
以前咱们使用RxJava + Retrofit + MVP的Demo,为了解决RxJava的内存泄漏, 使用了RxActivity,和RxFragment。this
经过上一篇Dagger2在Android中使用,是须要继承DaggerActivity 和 DaggerFragment的。。
这个时候怎么办呢。咱们来看咱们点开DaggerActivity的源码;
同时注意要给mPresenter加上@Inject标注,而后把咱们以前页面new的方法cretaePresenter删除。搞定了
public abstract class BaseDaggerActivity<T extends BasePresenter> extends RxFragmentActivity implements BaseView, HasAndroidInjector {
@Inject
public T mPresenter;
//public abstract T cretaePresenter();
...//省略部分代码,便于理解
}
复制代码
作完以上就搞定了,BaseDaggerFragment也是同样,若是须要注解的只要继承BaseDaggerActivity,不须要注解的话只要继承BaseActivity,便可。
以上就是用Dagger2 依赖注入Presenter。
以上操做清楚后,我在Demo里的POSTFragment,用了依赖注入,代码以下:
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() + "");
}
}
复制代码
toast结果是 :100;
咱们看看Woman的代码,注入了Soul
public class Woman {
@Inject
Soul soul;
@Inject
public Woman() {
}
public Soul getSoul() {
return soul;
}
public void setSoul(Soul soul) {
this.soul = soul;
}
}
复制代码
咱们看看Soul的代码,固然你能够用Module传值。
public class Soul {
private int money = 100;
@Inject
public Soul() {
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
复制代码
这样写完Woman类,和Soul类。不用作别的操做,就能用了。固然若是用Module传值,要在AppComponent写上!!是否是很方便很方便。
花了老大劲,别误会,不是要钱。能不能留下个足迹star啊