欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

在Android開發(fā)中使用Dagger2的方法

在Android開發(fā)中使用Dagger2的方法?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設計、網(wǎng)站制作與策劃設計,正定網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設10余年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:正定等地區(qū)。正定做網(wǎng)站價格咨詢:18982081108

使用方法如下:

@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開發(fā)中也那么火呢,于是在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.編寫實現(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<&#63; 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實現(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);//最好結合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);
 }
}

看完上述內(nèi)容,你們掌握在Android開發(fā)中使用Dagger2的方法的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享標題:在Android開發(fā)中使用Dagger2的方法
文章源于:http://chinadenli.net/article28/jijojp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈外貿(mào)網(wǎng)站建設服務器托管移動網(wǎng)站建設網(wǎng)站營銷虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)