最新国产好看的视频,伊人天堂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

簡(jiǎn)單解釋下,MyAppModule提供全局單例功能,比如打印日志,ActModule提供Activity級(jí)別的功能比如發(fā)起網(wǎng)絡(luò)請(qǐng)求(只是舉個(gè)栗子),現(xiàn)在我們希望在發(fā)起網(wǎng)絡(luò)請(qǐng)求的時(shí)候打印日志,那么解決方法也很簡(jiǎn)單——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若同我們?cè)诓襟E2中定義的一樣,不管在類中還是在其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ù)雜啦”的感覺呢...

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

參考文章

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

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

相關(guān)文章

最新評(píng)論

阳曲县| 沽源县| 大余县| 东乡族自治县| 启东市| 泰来县| 洪洞县| 石楼县| 河北区| 定日县| 武城县| 永兴县| 石台县| 博湖县| 湛江市| 伊金霍洛旗| 洛川县| 当雄县| 蒙山县| 东源县| 宜州市| 翁牛特旗| 漾濞| 柞水县| 玛多县| 萨迦县| 五台县| 邵阳市| 故城县| 揭西县| 浦江县| 张北县| 闵行区| 阳朔县| 尚义县| 绍兴县| 万州区| 福贡县| 清水河县| 北碚区| 石屏县|