详解Dagger2在Android开发中的新用法
本文假设读者已经有一定Dagger2使用经验
使用疑惑
之前工作中一直在使用dagger2进行开发,用起来确实很爽,但是我从我第一次使用我就一直有一个问题或者说疑问(本人才疏学浅脑子不够使),通常情况下我们有如下清单
MyApplication,MyAppComponent,MyAppModule
ActActivity,ActComponent,ActModule
简单解释下,
MyAppModule提供全局单例功能,比如打印日志,
ActModule提供
Activity级别的功能比如发起网络请求(只是举个栗子),现在我们希望在发起网络请求的时候打印日志,那么解决方法也很简单――
SubComponent或者
Component(dependencies=X.class)
于是我们首先在MyApplication中初始化MyAppcomponent(使用抽象类实现单例)
@Component(modules = MyAppModule.class) public abstract class MyAppComponent { ...... //使用SubComponent功能来完成component的组合 abstract ActComponent plus(); }
@Subcomponent(modules = ActModule.class) public interface ActComponent { void inject(ActActivity act); }
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); MyAppComponent.getInstance().inject(this); } }
然后就是就在Activity中使用ActComponent来提供注入功能,代码看上去就像如下...
MyAppComponent.getInstance() .plus() .inject(this);
为神马我使用的明明是
ActComponent,关
MyAppComponent什么事?(我最开始学习使用dagger2的时候完全无法接受这种写法),而且这似乎不太符合依赖注入的一个根本原则
a class shouldn't know anything about how it is injected.
新用法
谷歌爸爸很明显也注意到了这个问题,谁叫Dagger2在Android开发中也那么火呢,于是在Dagger2新版本中我们有了一个新东西dagger.android
Gradle引入方式
//dagger2 compile 'com.google.dagger:dagger:2.11' compile 'com.google.dagger:dagger-android:2.11' compile 'com.google.dagger:dagger-android-support:2.11' annotationProcessor 'com.google.dagger:dagger-compiler:2.11' annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
Demo地址在 https://github.com/hanliuxin5/Dagger2-demo
结合Demo和官方文档粗略翻译如下
1、在AppComponent中安装AndroidInjectionModule
@Component(modules = {AndroidInjectionModule.class}) public interface AppComponent { //.... }
2.编写实现了AndroidInjector<YourActivity>的Lychee3Activity
@Subcomponent(modules = ...) public interface ActSubComponent extends AndroidInjector<Lychee3Activity> { @Subcomponent.Builder public abstract class Builder extends AndroidInjector.Builder<Lychee3Activity> { } }
3.定义了ActSubComponent后,将其安装在绑定了ActSubComponent.Builder的Module中,并且将该Module安装在我们的AppComponent中
@Module(subcomponents = {ActSubComponent.class}) public abstract class BuildersModule { @Binds @IntoMap @ActivityKey(Lychee3Activity.class) abstract AndroidInjector.Factory<? extends Activity> lychee3Activity(ActSubComponent.Builder builder); }
@Component(modules = {AndroidInjectionModule.class, BuildersModule.class}) public interface AppComponent { //.... }
但是如果你的
ActSubComponent若同我们在步骤2中定义的一样,不管在类中还是在其Builder中没有的方法和超类型,你可以用下面的代码跳过2,3步骤
原文 Pro-tip: If your subcomponent and its builder have no other methods or supertypes than the ones mentioned in step #2, you can use @ContributesAndroidInjector to generate them for you
@ContributesAndroidInjector abstract Lychee2Activity lychee2Activity();
4.让你的MyApplication实现HasActivityInjector,并且注入DispatchingAndroidInjector,
public class MyApplication extends Application implements HasActivityInjector { @Inject DispatchingAndroidInjector<Activity> dispatchingAndroidInjector; @Override public void onCreate() { super.onCreate(); DaggerAppComponent.builder().AppContent(this).build().inject(this);//最好结合demo来看,不然AppContent是啥你不知道 } @Override public AndroidInjector<Activity> activityInjector() { return dispatchingAndroidInjector; } }
5.最后,在你Lychee3Activity和Lychee2Activity中的onCreate中,调super.onCreate()之前调用AndroidInjection.inject(this);
public class Lychee2Activity extends AppCompatActivity { public void onCreate(Bundle savedInstanceState) { AndroidInjection.inject(this); super.onCreate(savedInstanceState); } }
至此,新东西的使用差不多就到这了,但是为什么我会有一种“天,怎么越来越复杂啦”的感觉呢...
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
参考文章
https://google.github.io/dagger//android.html
https://android.jlelse.eu/android-and-dagger-2-10-androidinjector-5e9c523679a3
您可能感兴趣的文章:
- 【Android 应用开发】Android - TabHost 选项卡功能用法详解
- Android开发之DatePickerDialog与TimePickerDialog的功能和用法详解
- Android开发——DiskLruCache用法详解以及工作原理深度解析
- Android开发——DiskLruCache用法详解以及工作原理深度解析
- Android开发之Animations动画用法实例详解
- 【Android 应用开发】Android - TabHost 选项卡功能用法详解
- android开发中SharedPreferences用法详解(含源代码和运行结果截图)
- Android游戏开发学习之引擎用法实例详解
- App开发日报 2015-05-19 Android依赖注入框架Dagger2详解
- Android开发之recycleView详解代码,看完包你熟练掌握recycleView的用法。转自网络经典文章
- android开发教程之自定义属性用法详解
- Android开发之AlarmManager的用法详解
- android应用开发详解(八)---------------常用组件之选项卡Tab的三种用法(续)
- Android编程开发之NotiFication用法详解
- Android游戏开发教程之三:View类用法详解
- android开发之interpolator的用法详解
- android开发之interpolator的用法详解
- android开发之interpolator的用法详解
- Android开发之TimePicker控件用法实例详解
- Android开发之浏览器用法实例详解(调用uc,opera,qq浏览器访问网页)