我有一个具有以下结构的示例应用程序; MainActvity
显示包含LeftFragment
的{{1}}。我的目标是在LeftPresenter
中用Bundle
实例化片段,并将该MainActivity
传递给演示者,而无需在片段中调用Bundle
。
我想做什么:
Fragment.arguments
片段和演示者都分成@Inject
MainActivity
演示者进入@Inject
LeftFragment
片段的@Inject
本身,然后调用其展示者的方法,该方法采用Bundle
作为参数或直接Bundle
@Inject
进入展示者。>
实施
实现了Dagger 2(无Bundle
)以注入实例,如下所示:
添加了dagger.android
。暴露的MainActivityComponent
可在从属组件中访问。 LeftPresenter
具有提供MainActivityComponent
的模块。在MainActivity中构建了LeftPresenter
。
以MainActivityComponent
的形式添加了从属组件,该组件具有LeftComponent
方法来绑定片段实例和Builder
,并具有一种提供方法,该方法将片段作为参数并返回其{{1 }}。在片段中构建LeftModule
只要未将片段Bundle
划分为LeftComponent
,此配置就起作用。如果我尝试将片段注入到@Inject
中,它将失败,因为片段应注入一个捆绑包,而无法在MainActivity
中找到它,因为MainActivity
由MainActivityComponent
提供。
假想的解决方案和问题
只需将Bundle
的提供方法从LeftComponent
移到Bundle
就可以解决问题,我可以将片段注入MainActivty。但是我既不能也不想这样做。
“不想要”这样做是因为在我的实际应用程序中(此问题是我的应用程序的简化版本),我在LefComponenet
中有多个片段,并且我不想对于返回MainActivityComponent
类型的每个提供方法,请使用MainActivity
“不能做”,因为我必须将片段实例绑定到@Qulifiers
中。由于该组件内置于Bundle
中,因此我无法提供MainActivityComponenet
的实例。
我应该放弃我想做什么部分中的某些内容,或者可以实现吗?我将不胜感激