最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Dagger2在Android開發(fā)中的新用法

 更新時(shí)間:2017年07月03日 14:17:15   作者:荔枝我大哥  
本篇文章主要介紹了Dagger2在Android開發(fā)中的新用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文假設(shè)讀者已經(jīng)有一定Dagger2使用經(jīng)驗(yàn)

使用疑惑

之前工作中一直在使用dagger2進(jìn)行開發(fā),用起來確實(shí)很爽,但是我從我第一次使用我就一直有一個(gè)問題或者說疑問(本人才疏學(xué)淺腦子不夠使),通常情況下我們有如下清單

MyApplication,MyAppComponent,MyAppModule
ActActivity,ActComponent,ActModule

簡單解釋下,MyAppModule提供全局單例功能,比如打印日志,ActModule提供Activity級別的功能比如發(fā)起網(wǎng)絡(luò)請求(只是舉個(gè)栗子),現(xiàn)在我們希望在發(fā)起網(wǎng)絡(luò)請求的時(shí)候打印日志,那么解決方法也很簡單——SubComponent或者Component(dependencies=X.class)

于是我們首先在MyApplication中初始化MyAppcomponent(使用抽象類實(shí)現(xiàn)單例)

@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,關(guān)MyAppComponent什么事?(我最開始學(xué)習(xí)使用dagger2的時(shí)候完全無法接受這種寫法),而且這似乎不太符合依賴注入的一個(gè)根本原則a class shouldn't know anything about how it is injected.

新用法

谷歌爸爸很明顯也注意到了這個(gè)問題,誰叫Dagger2在Android開發(fā)中也那么火呢,于是在Dagger2新版本中我們有了一個(gè)新東西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

結(jié)合Demo和官方文檔粗略翻譯如下

1、在AppComponent中安裝AndroidInjectionModule

@Component(modules = {AndroidInjectionModule.class})
public interface AppComponent {
 //....
}

2.編寫實(shí)現(xiàn)了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實(shí)現(xiàn)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);//最好結(jié)合demo來看,不然AppContent是啥你不知道
 }

 @Override
 public AndroidInjector<Activity> activityInjector() {
  return dispatchingAndroidInjector;
 }
}

5.最后,在你Lychee3Activity和Lychee2Activity中的onCreate中,調(diào)super.onCreate()之前調(diào)用AndroidInjection.inject(this);

public class Lychee2Activity extends AppCompatActivity {
 public void onCreate(Bundle savedInstanceState) {
 AndroidInjection.inject(this);
 super.onCreate(savedInstanceState);
 }
}

至此,新東西的使用差不多就到這了,但是為什么我會(huì)有一種“天,怎么越來越復(fù)雜啦”的感覺呢...

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

參考文章

https://google.github.io/dagger//android.html

https://android.jlelse.eu/android-and-dagger-2-10-androidinjector-5e9c523679a3

相關(guān)文章

最新評論

高陵县| 承德市| 察雅县| 多伦县| 开原市| 甘谷县| 三都| 阿勒泰市| 大同市| 平南县| 剑阁县| 恩施市| 公安县| 大兴区| 曲靖市| 深圳市| 扶余县| 宜君县| 建德市| 富源县| 翼城县| 达州市| 宁陵县| 内丘县| 泰安市| 延庆县| 济南市| 交口县| 旌德县| 塔城市| 开封县| 育儿| 庐江县| 托里县| 阜宁县| 陆良县| 栾城县| 大安市| 峡江县| 略阳县| 台安县|