Dagger 2.11 - 如何使Activity和Fragment共享共同的价值观?

时间:2017-11-05 14:59:55

标签: android dependency-injection kotlin dagger-2

我目前正在Android上学习Dagger 2,我想将Activity的注入注入片段,但我不知道如何。

这是一个工作设置,显示注入片段和活动的值。

很抱歉代码墙,我简化了我的代码,只包含相关部分。

@Singleton
@Component(modules = arrayOf(AndroidSupportInjectionModule::class, AppModule::class, BuildersModule::class))
interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun setApplication(application: Application): Builder

        fun build(): AppComponent

    }

    fun apiService() : ApiService

    fun inject(app: App)
}

@Module(subcomponents = arrayOf(MainActivitySubComponent::class, MainFragmentSubComponent::class))
class AppModule

@Module
abstract class BuildersModule {

    @Binds
    @IntoMap
    @ActivityKey(MainActivity::class)
    abstract fun bindMainActivity(builder: MainActivitySubComponent.Builder): AndroidInjector.Factory<out Activity>


    @Binds
    @IntoMap
    @dagger.android.support.FragmentKey(MainFragment::class)
    abstract fun bindMainFragment(builder: MainFragmentSubComponent.Builder): AndroidInjector.Factory<out Fragment>

}

在应用程序中创建AppComponent

class App : Application(), HasActivityInjector {

    @Inject
    var activityInjector: DispatchingAndroidInjector<Activity>? = null

    override fun onCreate() {
        super.onCreate()

        DaggerAppComponent
                .builder()
                .setApplication(this)
                .build()
                .inject(this)
    }

    override fun activityInjector(): AndroidInjector<Activity>? {
        return activityInjector
    }

}

MainActivity:

@Subcomponent(modules = arrayOf(MainActivityModule::class))
interface MainActivitySubComponent : AndroidInjector<MainActivity> {
    @Subcomponent.Builder
    abstract class Builder : AndroidInjector.Builder<MainActivity>()
}

@Module
class MainActivityModule {

    @Provides
    fun providePresenter(mainView: MainView, apiService: ApiService): MainPresenter
            = MainPresenterImpl(mainView, apiService)

}

class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {

    @Inject
    lateinit var mainPresenter: MainPresenter

    @Inject
    lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment>

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (savedInstanceState == null) {
            supportFragmentManager
                    .beginTransaction()
                    .add(R.id.container, MainFragment())
                    .commitAllowingStateLoss()
        }
    }

    override fun supportFragmentInjector() = fragmentInjector

}

MainFragment:

@Subcomponent(modules = arrayOf(MainFragmentModule::class))
interface MainFragmentSubComponent : AndroidInjector<MainFragment> {
    @Subcomponent.Builder
    abstract class Builder : AndroidInjector.Builder<MainFragment>()
}

@Module
class MainFragmentModule {

    @Provides
    fun providePresenter(mainView: MainView, apiService: ApiService): MainPresenter
            = MainPresenterImpl(mainView, apiService)
}

class MainFragment : Fragment() {

    @Inject
    lateinit var mainPresenter: MainPresenter

    override fun onAttach(context: Context?) {
        AndroidSupportInjection.inject(this)
        super.onAttach(context)
    }
}

所以我发现活动和片段如何使用相同的ApiService类。 我想在MainPresenterMainActivity中注入相同的MainFragment实例。

如何在不更改MainPresenter中的代码的情况下访问活动注入的MainFragment?基本上,我不希望MainFragment知道它是如何注入的。

0 个答案:

没有答案