dagger.android--Fragment,BaseFragment

2020-11-22 21:00:28

参考地址 Android开发之dagger.android--Fragment

前言

在上一篇文章中 Android开发之dagger.android--Activity,主要是使用Dagger-Android注入到Activity中,上文结尾也说到,Dagger-Android也可以注入到Fragment,BroadcastReceiver等,这篇文章就就讨论使用Dagger-Android注入到Fragment和BroadcastReceiver。
详情请参照Dagger-Android官方文档 官网传送门

代码已提交到github
代码传送门

注入Fragment实例

注入Fragment和注入Activity是一样的,以相同的方式定义subcomponent,不同有以下几点是不同的。

  • 1 使用Fragment参数来代替Activity参数

  • 2 使用 @FragmentKey来代替@ActivityKey

  • 3 使用HasFragmentInjector来代替@HasActivityInjector

  • 4 AndroidInjection.inject(Fragment)方法,在Fragment的onAttach()中调用,而不是在onCreate()中

  • 5 Fragment的Module添加位置,和Activity是不同的,它取决于Fragment需要的其他依赖注入

和Activity定义modules的不同,你可以选择在什么地方为Fragment安装module模块,你可以使你的Fragment component成为另一个Fragment component, Activity component, 或者 Application component 的subcomponent ,这一切都依赖于你的Fragment需要的其他绑定。在确定了component 的位置之后,使相应的类型实现HasFragmentInjector。

上代码

步骤一: 和Fragment绑定的Activity实现HasFragmentInjector接口
在这里主要完成:

  1. 在super.onCreate(savedInstanceState);前完成AndroidInjection.inject(this); 实现Activity的注入。

  2. 实现supportFragmentInjector()方法,返回fragmentInjector。

public class DaggerFragmentActivity extends AppCompatActivity implements HasSupportFragmentInjector{

    @Inject
    DispatchingAndroidInjector<Fragment> fragmentInjector;

    ViewPager viewPager;
    ArrayList<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        AndroidInjection.inject(this);  // 实现Activity的注入

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dagger_fragment);
        init();

    }

    private void init() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        // 初始化ViewPager对象中所需要的数据
        list = new ArrayList<Fragment>();
        list.add(new Fragment01());
        list.add(new Fragment02());
        list.add(new Fragment03());
        list.add(new Fragment04());

        viewPager.setAdapter(new MyAdapterState(getSupportFragmentManager()));
    }


  // 返回fragmentInjector
    @Override
    public AndroidInjector<Fragment> supportFragmentInjector() {
        return fragmentInjector;
    }}

注意:

  • 1 如果使用v4包中的Fragment(既android.support.v4.app.Fragment),应实现HasSupportFragmentInjector接口,应该实现

 @Override
    public AndroidInjector<Fragment> supportFragmentInjector() {
        return fragmentInjector;
    }
  • 2 如果使用普通的Fragment(即android.app.Fragment),应该实现HasFragmentInjector接口,应该实现

 @Override
    public AndroidInjector<android.app.Fragment> fragmentInjector() {
        return fragmentInjector;
    }

步骤二: 在Fragment01的onAttach()方法中,super.onAttach(context)方法前调用AndroidSupportInjection.inject(this)。

public class Fragment01 extends Fragment {

    private View mView;

    private TextView tv;

    @Inject
    Student mStudent;

    @Override
    public void onAttach(Context context) {
        AndroidSupportInjection.inject(this);  //  在这里完成
        super.onAttach(context);
    }

  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView=inflater.inflate(R.layout.fragment_fragment01, container, false);
        Log.d("hbj--",mStudent.toString());
        tv=(TextView)mView.findViewById(R.id.frg_text);
        tv.setText(mStudent.showMessage());
        return mView;
    }}

注意:

  • 如果使用v4包中的Fragment,在onAttach()方法中,应调用AndroidSupportInjection.inject(this);

  • 如果使用普通的Fragment(即android.app.Fragment),在onAttach()方法中,应调用AndroidInjection.inject(this);
    步骤三:创建 Fragment对应的子组件Subcomponent,继承自AndroidInjector,子组件中含有一个抽象类Builder, 该Builder继承自AndroidInjector.Builder,并由@Subcomponent.Builder注解。

@Subcomponent(modules = {AndroidInjectionModule.class, Fragment01Module.class})public interface DaggerFragmentActSubComponent extends AndroidInjector<DaggerFragmentActivity>{

    @Subcomponent.Builder     abstract class Builder extends AndroidInjector.Builder<DaggerFragmentActivity>{

    }}

步骤四:创建 Fragment对应的Module,关联subcomponents,在这里可以提供Fragment01需要的实例(这里提供的是Student实例)。

@Module(subcomponents = Fragment01Subcomponent.class)public abstract class Fragment01Module {
    @Binds
    @IntoMap
    @FragmentKey(Fragment01.class)
    abstract AndroidInjector.Factory<? extends Fragment> bindFragment01InjectorFactory(Fragment01Subcomponent.Builder builder);

    @Provides
    static Student provideStudent() {
        return new Student();
    }}

步骤五:将该Module注入到和Fragment01绑定的Activity的子组件SubComponent中。以下是和Fragment01关联的SubComponent和Module,参见是上篇文章 Android开发之dagger.android--Activity的用法

@Subcomponent(modules = {AndroidInjectionModule.class, Fragment01Module.class})public interface DaggerFragmentActSubComponent extends AndroidInjector<DaggerFragmentActivity>{

    @Subcomponent.Builder     abstract class Builder extends AndroidInjector.Builder<DaggerFragmentActivity>{

    }}

@Module(subcomponents = DaggerFragmentActSubComponent.class)public abstract class DaggerFragmentActModule {

    @Binds
    @IntoMap
    @ActivityKey(DaggerFragmentActivity.class)
    abstract AndroidInjector.Factory<? extends Activity> bindDaggerFragmentActivityInjectorFactory(DaggerFragmentActSubComponent.Builder builder);}

编译运行,可以看到Fragment01实例注入成功。

进一步改进

参照上篇文章进一步改进,直接上代码。

  • 1 新建BaseFragment,在BaseFragment里执行 AndroidInjection.inject(this)或者 AndroidSupportInjection.inject(this);

public class BaseFragment extends Fragment{

    @Override
    public void onAttach(Context context) {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
    }}

并将所有Fragment继承自BaseFragment

public class Fragment01 extends BaseFragment {
  • 2 新建BaseFragmentSubcomponent,在这里我们AndroidInjector.Builder的是上部新建的BaseFragment。

@Subcomponent(modules = {AndroidInjectionModule.class})public interface BaseFragmentSubcomponent extends AndroidInjector<BaseFragment>{
    @Subcomponent.Builder
    public abstract class Builder extends AndroidInjector.Builder<BaseFragment>{

    }}
  • 3 新建一个AllFragmentsModule,子部件为上部新建的BaseFragmentSubcomponent,在这里提供所有的AFragmentModule,每个都用@ContributesAndroidInjector注解。

@Module(subcomponents = BaseFragmentSubcomponent.class)public abstract class AllFragmentsModule {

    @ContributesAndroidInjector(modules = Fragment01ProModule.class)
    abstract Fragment01 contributeFragment01Injector();}
  • 4 修改Fragment01ProModule,变成一个普通的Module,和前文对比,删掉了(subcomponents = Fragment01Subcomponent.class)以及bindFragment01InjectorFactory抽象方法。在这里可以根据需要提供实例。

@Modulepublic abstract class Fragment01ProModule {

    @Provides
    static Student provideStudent() {
        return new Student();
    }
  • 5  修改AppComponent,把AllFragmentsModule.class放进modules中。

@Singleton@Component(modules = {AndroidInjectionModule.class, AppModule.class, AllActivitysModule.class, AllFragmentsModule.class})public interface AppComponent {
    void inject(MyApplication application);}

编译以后可是可以运行的。

总结

经过改进,如果我们新添了新的Fragment,我们只要下面的步骤:

  • 1.新Fragment继承BaseFragment。


    1. 为新Fragment建一个NewFragmentModule,这是一个普通的Module,用@Module注解,在这提提供新Fragment需要的实例。注意要用static关键字哦。

  • 3 新Fragment添加到AllFragmentsModule 中,类似于下面的代码。

@Module(subcomponents = BaseFragmentSubcomponent.class)public abstract class AllFragmentsModule {

    @ContributesAndroidInjector(modules = Fragment01ProModule.class)
    abstract Fragment01 contributeFragment01Injector();

    @ContributesAndroidInjector(modules = Fragment02ProModule.class)
    abstract Fragment02 contributeFragment02Injector();}

参照官网,经过改进,注入过程更加简洁。
代码传送门

Dagger-Android还提供了BroadcastReceiver,Service等其他组件的注入方式,具体可以参照官网 官网传送门
,我们就不再讨论了。



  • 2019-05-09 11:46:30

    Glide使用高级技巧(解决Glide生成缓存Key问题)

    虽说Glide将缓存功能高度封装之后,使得用法变得非常简单,但同时也带来了一些问题。 比如之前有一位群里的朋友就跟我说过,他们项目的图片资源都是存放在七牛云上面的,而七牛云为了对图片资源进行保护,会在图片url地址的基础之上再加上一个token参数。也就是说,一张图片的url地址可能会是如下格式:

  • 2019-05-13 14:34:42

    linux系统中清理MySql的日志文件,打印日志文件

    默认情况下mysql会一直保留mysql-bin文件,这样到一定时候,磁盘可能会被撑满,这时候是否可以删除这些文件呢,是否可以安全删除,是个问题。 首先要说明一下,这些文件都是mysql的日志文件,如果不做主从复制的话,基本上是没用的,虽然没用,但是不建议使用rm命令删除,这样有可能会不安全,正确的方法是通过mysql的命令去删除。

  • 2019-05-14 16:47:27

    数据库整理碎片

    最后还是用的ALTER TABLE来修改的,不知道为什么有时候管用,有时候不管用。

  • 2019-05-17 16:27:26

    在vue项目里面使用引入公共方法

    今天早上来到公司,没事看了一下别人的博客,然后试了一下,发现的确是可以的,在此记录一下,方便自己日后查阅。 首先新建一个文件夹:commonFunction ,然后在里面建立 一个文件common.js

  • 2019-05-18 12:37:39

    Android夜间模式的实现方案

    对于一款阅读类的软件,夜间模式是不可缺少的。最初看到这个需求时候觉得无从下手,没有一点头绪。后来通过查阅资料发现Android官方在Support Library 23.2.0中已经加入了夜间主题。也就是只需要通过更换主题便可实现日间模式和夜间模式的切换。下面截取项目实现的夜间模式效果图:

  • 2019-05-18 12:38:41

    android 快速实现夜间模式

    最近项目中遇到了一个问题,夜间模式在8.0以上的手机中不起作用,查看了一下原因,是夜间模式实现方法的问题。分两种情况介绍一下

  • 2019-05-18 12:40:35

    Android夜间模式的几种实现

    通过增加一层遮光罩来实现。效果不是很理想,但是好用,毕竟很多手机都有自己的夜间模式了

  • 2019-05-19 02:25:15

    php使用TCPDF生成PDF文件教程

    orientation属性用来设置文档打印格式是“Portrait”还是“Landscape”。 Landscape为横式打印,Portrait为纵向打印