我想实现应用程序的一部分,该部分将采取一些由演示者处理的步骤。我已经声明了一个范围:
@Scope
annotation class FormScope
接下来,我想声明一个模块,该模块将提供必要的依赖项:
@Module
object FormModule {
@JvmStatic
@Provides
fun providesFragmentManager(activity: FragmentActivity): FragmentManager = activity.supportFragmentManager
@JvmStatic
@Provides
fun providesNavigation(fragmentManager: FragmentManager): SobergridCoachingNavigationUnauthorizedFirstStep = SobergridCoachingNavigationUnauthorizedFirstStep(fragmentManager)
@JvmStatic
@Provides
fun providesCoordinator(navigation: NavigationUnauthorized): CoordinatoUnauthorized = CoordinatoUnauthorized(navigation)
@JvmStatic
@Provides
@Reusable
fun providesPresenter(coordinator: CoordinatoUnauthorized): OnboardingFragmentContract.Presenter = FormPresenter(coordinator)
}
最后,我将模块绑定到片段中,然后将依赖项注入到这些片段中:
@Module(includes = [AndroidSupportInjectionModule::class])
abstract class FragmentBindingModule {
@FormScope
@ContributesAndroidInjector(modules = [FormFirstModule::class, FormModule::class])
abstract fun contributesFormFirst(): FormFirstFragment
@ContributesAndroidInjector(modules = [FormSecondModule::class, FormModule::class])
abstract fun contributesFormSecond(): FormSecondFragment
@ContributesAndroidInjector(modules = [FormThirdModule::class, FormModule::class])
abstract fun contributesFormThird(): FormThirdFragment
}
我遇到的问题是,每次显示新片段时,Dagger都会创建一个Presenter的新实例。我想为所有这些片段使用一个演示者。我做错了什么?我应该改善什么才能实现自己的目标?
更新
我还尝试用@Singleton
注释我的provide方法
@JvmStatic
@Provides
@Signleton
fun providesPresenter(coordinator: CoordinatoUnauthorized): OnboardingFragmentContract.Presenter = FormPresenter(coordinator)
但这会导致编译错误。我尝试的最后一件事是在Presenter类的声明之前放置注释(@Reusable
和@Singleton
)。这种方法不会给我带来任何编译错误,但是Presenter类的实例仍然不止一个。
答案 0 :(得分:0)
将您的Presenter提供给具有Scope的FragmentActivity模块,以便为Activity中的所有片段获得相同的Presenter
@Module
public class FragmentActivityModule {
//common provides for all fragments
@Provides
@FormScope
public YourPresenter providesYourPresenter() {
return new YourPresenter();
}
....
您的建造者必须看起来像这样
@Module
public abstract class ActivityBuilder {
@FormScope
@ContributesAndroidInjector(modules = {FragmentActivityModule.class, Form1FragmentProvider.class
, Form2FragmentProvider.class})
abstract FragmentActivity bindFragmentActivity();
在Form1FragmentModule中仅为Form1Fragment提供。 为所有片段Form1FragmentProvider,Form2FragmentProvider ...创建FragmentProviders ...
@Module
public abstract class Form1FragmentProvider {
@ContributesAndroidInjector(modules = Form1FragmentModule.class)
abstract Form1Fragment provideForm1FragmentFactory();
}
不要忘记在FragmentActivity中实现HasSupportFragmentInjector
public class FragmentActivity extends AppCompatActivity implements HasSupportFragmentInjector {
@Inject
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;
....
@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
return fragmentDispatchingAndroidInjector;
}
别忘了在片段中附加AndroidSupportInjection
@Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
}