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等其他组件的注入方式,具体可以参照官网 官网传送门
,我们就不再讨论了。



  • 2018-08-07 20:00:42

    xUtils3.0版本的发送同步网络请求的方式

    对于Android开发来说,基本都是用异步来从网络上请求数据,很少用到同步请求的。近日项目有个地方需要使用到同步请求(以我目前的知识储备来说好像只能用同步请求来解决这个问题了),去网上搜索相关资料,又没有找到什么明确的使用方法。所以记下来,以备不时之需。

  • 2018-08-14 23:35:28

    Retrofit 设置 超时时间

    今天开发的时候遇到一个网络请求超时的问题,后台处理是成功的,但是移动端返回的总是提示请求超时,在设置了retrofit请求超时的时间延长以后,就可以请求成功了,下面是配置的方法:

  • 2018-08-16 16:10:43

    Laravel 跨域解决方案

    我们在用 laravel 进行开发的时候,特别是前后端完全分离的时候,由于前端项目运行在自己机器的指定端口(也可能是其他人的机器) , 例如 localhost:8000 , 而 laravel 程序又运行在另一个端口,这样就跨域了,而由于浏览器的同源策略,跨域请求是非法的。其实这个问题很好解决,只需要添加一个中间件就可以了。

  • 2018-08-18 20:30:12

    laravel5.5 路由分割成不同文件

    routes.php/api.php文件用来放置laravel路由,当项目越来越大,相应的路由文件也会越来越多。如果能够将不同功能的路由分割到不同的文件,那么对以后的维护将很有帮助。

  • 2018-08-20 15:26:19

    关于OnTouch 和OnClick同时调用冲突的解决方案

    大家在搞轮播图的时候会碰到这样的情况,点击进入webview界面,长按轮播图停止轮播,手松开图又开始轮播,这里就涉及到了OnTouch 和OnClick同时调用。两者是有冲突的。这里简单介绍,给大家提供思路。