我有一个要用作单例的模型(AModel)。我创建了一个名为@ApplicationScope的自定义范围,以将其用于只需要使用一次的每个类。因此,AppComponent和AModel共享此@ApplicationScope。我有一些片段(ConfirmationFragment),我想同时使用AModel和BModel。 BModel具有不同的范围,因为我想在3个片段中使用它,但是Amodel到处都需要。
访问AModel和BModel的想法是让ConfirmationComponent依赖于已经存在AModel的AppComponent。这样,如果我将ConfirmationComponent注入(inject)到ConfirmationFragment中,我也可以使用AModel。
但是我遇到了以下错误:[Dagger/IncompatiblyScopedBindings] ConfirmationComponent scoped with @ConfirmationScope may not reference bindings with different scopes:
当从ConfirmationFragment中注释掉注入(inject)AModel时,构建成功,但如果不是,则构建失败。我也需要该片段中的AModel。
我该如何解决这个问题?
(以防万一,这很重要:我只使用一个 Activity ,然后让Android导航对片段进行处理。)
open class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val appComponent = initializeComponent()
}
val appComponent: AppComponent by lazy {
initializeComponent()
}
val confirmationComponent: ConfirmationComponent by lazy {
initializeConfirmationComponent()
}
open fun initializeComponent(): AppComponent {
return DaggerAppComponent.factory().create(applicationContext)
}
open fun initializeConfirmationComponent(): ConfirmationComponent {
return DaggerConfirmationComponent.builder().appComponent(appComponent).build()
}
}
@ApplicationScope
@Component(modules = [NetworkModule::class])
interface AppComponent {
@Component.Factory
interface Factory {
fun create(@BindsInstance context: Context) : AppComponent
}
fun inject(activity: MainActivity)
fun inject(fragment: ConfirmationFragment)
}
@ConfirmationScope
@Component(dependencies = [AppComponent::class])
interface ConfirmationComponent {
fun inject(fragment: ConfirmationFragment)
}
@ApplicationScope
class AModel @Inject constructor() {}
@ConfirmationScope
class BModel @Inject constructor() {}
class ConfirmationFragment : Fragment() {
@Inject
lateinit var modelA : AModel
@Inject
lateinit var modelB : BModel
override fun onAttach(context: Context) {
super.onAttach(context)
(requireActivity().applicationContext as MyApplication).confirmationComponent.inject(this)
}
// Rest of the code
}
最佳答案
我认为您应该将其改写。如果我正确地理解了您,那么ModelA的范围比ModelB的范围大,这意味着您可以将ModelB作为ModelA的子组件,范围更窄。
因此,您需要:
//@YourScopeAnnotation
@Subcomponent(modules = [...]) // if it is dependent on any modules
interface ConfirmationComponent {
// needed for dagger to create component
@Subcomponent.Factory
interface Factory {
fun create(): ConfirmationComponent
}
fun inject(yourFragment: Fragment) // fun inject your fragment
}
@Module(
subcomponents = [ConfirmationComponent::class]
)
class SubcomponentsModule
//@ApplicationScopeAnnotation I think you can also use @Singleton
@Component(
modules = [NetworkModule::class, SubcomponentsModule::class]
)
interface ApplicationComponent {
fun inject(activity: MainActivity)
fun confirmationComponent(): ConfirmationComponent.Factory
}
@Inject
lateinit var modelA : AModel
lateinit var confirmationComponent: ConfirmationComponent
override fun onCreate(savedInstanceState: Bundle?) {
confirmationComponent = (applicationContext as MyApplication).appComponent
.confirmationComponent()
.create()
modelA = (applicationContext as MyApplication).appComponent.inject(this)
}
@Inject
lateinit var modelB: ModelB // inject modelB
lateinit var modelA: ModelA // get ModelA from activity where it was already injected
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activity as MainActivity).confirmationComponent.inject(this)
modelA = (activity as MainActivity).modelA
}
我希望这有帮助 :)