我在我的项目中使用dagger2。项目遵循MVP模式。我的应用程序设计就像我正在使用一个活动
在我的申请中。所有UI元素都在Fragments中,并且单个活动充当
项目中所有片段的主机活动。我项目中的每个模块都将使用
作为UI组件的片段,例如LoginFragment,SplashScreenFragment,RegistrationFragment,HomeScreenFragment。

因此,基本上我的活动只会启动一次,并且仅包含一个片段容器,在该片段容器中将替换片段
当用户导航到每个模块中的每个屏幕时。

每次用户从一个屏幕移动到另一个屏幕时,它都不会调用它创建活动,而只会调用gotoNextScreen
在活动中,然后将下一个片段膨胀

因此,我的活动将仅执行以下操作


接收要启动的下一个屏幕类型
从启动屏幕类型中查找要放大的片段
从意图
创建片段(在MVP中充当视图层)和演示者
用于屏幕
链接演示者和片段(或视图)
执行片段交易。


由于我的屏幕数量为n(fragmetns和演示者的数量为n),因此无法使用
注射类似的活动

            public class MainActivity{
                @Inject RegistrationFragment registrationFragment;
                @Inject LoginFragment loginFragment
            }


因为场的数量巨大,所以一次只需要一个片段
和一位主持人。所以我要做的是,我为每个模块创建了单独的组件,
使其成为我的主要App组件的从属组件。现在我的活动看起来像这样。

            public void gotoNextScreen(ScreenType screentype, Data data){

                        switch(screentype){
                            case LOGIN:
                                 DaggerLoginComponent.builder()
                                                .appComponent(mAppComponent)
                                                .build();
                                 // get login fragment and presenter from login component and inflate
                                 break;

                            case REGISTRATION:
                                 DaggerLoginComponent.builder()
                                                .appComponent(mAppComponent)
                                                .build();
                               // get registration fragment and presenter from registration component and inflate

                             // and the switch case goes on like this

                        }
        }


我发现这很困难,因为我几乎要在每个模块中重复执行代码。
我为不同的脆弱类型和演示者创建了不同的组件。

我可以简化泛型之类的使用吗?匕首是否提供解决这种问题的任何简单方法
问题?谁能显示一些示例代码来解决这个问题?

最佳答案

我也不知道这是否是正确的方法,因为我对Dagger2和MVP也很陌生,但是我已经实现了它:

片段具有对的回调,如official documentation中所述。

回调为gotoNextScreen方法。
goToNextScreen(由Activity实现),调用Activity演示者。

该演示者负责实例化Fragment,并在View(活动)上调用displayFragment方法。这样,活动对实现一无所知。它只是将信息传递给演示者,然后显示片段。

看起来像这样:

片段A

public void onNext(){
   mActivityCallback.gotoNextScreen(screentype, data);
}


活动

public void gotoNextScreen(ScreenType screentype, Data data){
   mPresenter.goToNext(screentype, data);
}

public void displayFragment(Fragment f){
   // Code to replace the current Fragment by the fragment f
}


活动主持人

public void goToNext(ScreenType screentype, Data data){
   Fragment f;
   switch(screentype){
      case A:
          f = FragmentA.newInstance(data);
          break;
      case B:
          f = FragmentB.newInstance(data);
          break;
      ...
   }
   mView.displayFragment(f); // mView is the Activity
}


如我所说,我不确定此实现是否有效,因为:


您有2次相同的gotoNextScreen方法(在“活动”和“演示者”中)
演示者处理Fragment(Android对象)。
片段未注入。


编辑

匕首部分:

创建活动时将其注入:

MainActivityComponent component = ((CustomApplication) getApplication()).getComponent()
                .plus(new MainActivityModule());


与片段相同:

活动的回调具有getComponent方法,并且在附加Fragment时,它将调用:

public void onAttach(Context context) {
   //...
    activityCallback.getComponent().inject(FragmentA.this)
}

10-08 18:13