dagger android support to androidx.fragment

后端 未结 7 1861
孤城傲影
孤城傲影 2021-02-07 02:47

How to inject a fragment from the package androidx.fragment.app.Fragment ?

I\'m using the dagger-android framework to inject my dependencies in

7条回答
  •  天涯浪人
    2021-02-07 03:24

    The solution to my particular problem was to use android dagger classes as interfaces instead of extend of them:

    class MyFragment() : HasSupportFragmentInjector {
    
        @Inject
        lateinit var childFragmentInjector: DispatchingAndroidInjector
    
        override fun onAttach(context: Context?) {
            AndroidSupportInjection.inject(this)
            super.onAttach(context)
        }
    
        override fun supportFragmentInjector(): AndroidInjector {
            return childFragmentInjector
        }
    
    ........
    
    }
    

    To my Activities

    class MyActivity : HasSupportFragmentInjector {
        @Inject
        internal lateinit var fragmentInjector: DispatchingAndroidInjector
    
        override fun onCreate(savedInstanceState: Bundle?) {
            AndroidInjection.inject(this)
            super.onCreate(savedInstanceState)
        }
    
        override fun supportFragmentInjector(): AndroidInjector = fragmentInjector
    

    ...... }

    and also I have this in my gradle.properties file:

    android.useAndroidX = true android.enableJetifier = true

提交回复
热议问题