Search code examples
androiddagger-2android-viewmodelandroidinjector

Why AdnroidInjector.inject(fragment) uses deprecated android.support.v4.app.Fragment


I'm using Dagger2 for DI in android app,

I want inject viewModel in a fragment by AndroidInjector and have this two line :

 AndroidInjection.inject(this)
 viewModel = ViewModelProviders.of(this, viewModelFactory).get(ProductDetailViewModel::class.java)

When my fragment extends androidx.fragment.app.Fragment shows this error:

  • the first line uses deprecated android.support.v4.app.Fragment
  • but second uses android.app.Fragment

now I don't know my fragment should extends which one!

I get this error when choose one of them :

None of the following functions can be called with the arguments supplied: public open fun inject(activity: Activity!): Unit defined in dagger.android.AndroidInjection public open fun inject(fragment: Fragment!): Unit defined in dagger.android.AndroidInjection public open fun inject(service: Service!): Unit defined in dagger.android.AndroidInjection public open fun inject(contentProvider: ContentProvider!): Unit defined in dagger.android.AndroidInjection


Solution

  • My mistake was using dagger-android (not dagger-android-support), and dagger-android just has :

     AndroidInjection.inject(this)
    

    but I should use

     AndroidSupportInjection.inject(this)
    

    that is in supportLibrary version of dagger-android that named dagger-android-support