温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在Android开发中使用Dagger2的方法

发布时间:2020-12-08 17:12:38 来源:亿速云 阅读:661 作者:Leah 栏目:移动开发

在Android开发中使用Dagger2的方法?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

使用方法如下:

@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<&#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实现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);
 }
}

看完上述内容,你们掌握在Android开发中使用Dagger2的方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI