简体   繁体   English

在匕首2中发现了一个依赖循环

[英]Found a dependency cycle in dagger 2

I try to provide fragment with DatePickerDialog , but when i use the BasicInfoFragment as argument it cause the error : Found a dependency cycle why that happen ?! 我尝试为DatePickerDialog提供片段,但是当我使用BasicInfoFragment作为参数时,会导致错误: Found a dependency cycle为什么会发生?

@Module
class ActivityModule(val activity: AppCompatActivity) {

    @Provides
    fun provideActivity() = activity

    @Provides
    @ActivityContext
    fun provideContext(): Context = activity

    // [...]
    @Provides
    fun provideDatePickerDialog(basicInfoFragment: BasicInfoFragment): 
    DatePickerDialog {
        return DatePickerDialog(activity, basicInfoFragment, 1999, 9, 9)
    }
}

Fragment code : 片段代码:

class BasicInfoFragment @Inject constructor()
    : BaseFragment(), BasicInfoMvpView , 
      LabelledSpinner.OnItemChosenListener, 
      DatePickerDialog.OnDateSetListener {

    @Inject
    lateinit var datePickerDialog: DatePickerDialog

    @Inject
    lateinit var mPresenter: BasicInfoMvpPresenter<BasicInfoMvpView>

    // user info
    private lateinit var gender: DataManager.UserGender
    private var birthOfDate: Long = 0L


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.base_user_info_layout, container, false)

        activityComponent?.let {
            it.inject(this)
            mPresenter.onAttach(this)
        }

        return view
    }

    override fun onDateSet(dataPicker: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        birthOfDate = GregorianCalendar(year, month, dayOfMonth).timeInMillis
    }
}

In your provideDatePickerDialog you need a BasicInfoFragment , but then when you also try to inject a DatePickerDialog in BasicInfoFragment . 在您的provideDatePickerDialog您需要一个BasicInfoFragment ,但是当您还尝试在BasicInfoFragment注入DatePickerDialog时。 Hence the chicken or egg problem - to create either of them you need the other. 因此,鸡肉或鸡蛋的问题-要创建它们中的一个,就需要另一个。

Something else to take into account: in the constructor of the DateTimePicker you really need the DatePickerDialog.OnDateSetListener not the BasicInfoFragment . 需要考虑的其他事项:在DateTimePicker的构造函数中,您确实需要DatePickerDialog.OnDateSetListener而不是BasicInfoFragment

Solution 1 解决方案1

Change the provideDatePickerDialog as follows 更改providerDatePickerDialog,如下所示

@Provides
fun provideDatePickerDialog(basicInfoFragment: BasicInfoFragment): 
DatePickerDialog {
    return DatePickerDialog(activity)
}

then set the listener and the date in the fragment. 然后在片段中设置侦听器和日期。 See documentation here 在这里查看文档

Solution 2 解决方案2

Don't provide the DatePickerDialog with Dagger but instead create it in the BasicInfoFragment with this line: 不要为DatePickerDialog提供Dagger,而是在BasicInfoFragment中使用以下代码行创建它:

DatePickerDialog(activity, this, 1999, 9, 9)

This way you will also eliminate other possible issues and confusions. 这样,您还将消除其他可能的问题和困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM