Я использую Dagger2 для внедрения зависимостей в Android. У меня есть интерфейс с именем FragmentChangeListener, который реализован в MainActivity.Я пытаюсь предоставить экземпляр MainActivity с помощью @Binds
Annotation, но когда я использую @Inject fragmentChagneListener
во фрагменте, это выдает ошибку ошибки: [Dagger / MissingBinding] listeners.FragmentChangeListener не может быть предоставлен без метода @Provides-annotated
.
@Module
abstract class MainActivityModule {
@Module
companion object{
@Provides
@MainActivityScope
@JvmStatic
fun provideMainRepo(apiRequest: ApiRequest):MainActivityRepo{
return MainActivityRepo(apiRequest)
}
@Provides
@MainActivityScope
@JvmStatic
fun provideFragmentManager(mainActivity: MainActivity):
FragmentManager {
return mainActivity.supportFragmentManager
}
}
@Binds
abstract fun bindFragmentChangeListener(mainActivity: MainActivity):
FragmentChangeListener
}
abstract class ActivitiesModuleInjector {
@MainActivityScope
@ContributesAndroidInjector(
modules = [
MainActivityModule::class
]
)
abstract fun provideMainActivity():MainActivity
@ContributesAndroidInjector
abstract fun provideBaseActivity():BaseActivity
@SignInScope
@ContributesAndroidInjector(
modules = [
SignInModule::class
]
)
abstract fun providesSignInActivity():SignInActivity
@SplashScope
@ContributesAndroidInjector
abstract fun provideSplash(): Splash
}
class MainActivity: BaseActivity() ,FragmentChangeListener{
@Inject
@MainActivityScope
lateinit var fragmentManager: FragmentManager
lateinit var fragmentTransaction:FragmentTransaction
lateinit var binding:ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding=DataBindingUtil.setContentView(this,R.layout.activity_main)
}
override fun onFragmentChange(fragment: Fragment, bundle: Bundle?) {
fragment.arguments = bundle
fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.add(R.id.containerMainActivity,fragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
}
@Module
abstract class FragmentsModuleInjector {
@ContributesAndroidInjector
abstract fun provideBaseFragment():BaseFragment
@AddRideScope
@ContributesAndroidInjector(
modules = [AddRideModule::class]
)
abstract fun provideAddRideFragment():AddRide
}
class AddRide : BaseFragment() {
lateinit var rideBiding: FragmentAddRideBinding
@Inject
lateinit var fragmentChangeListener: FragmentChangeListener
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
rideBiding=DataBindingUtil.inflate<ViewDataBinding>(
inflater,
R.layout.fragment_add_ride,
container,false) as FragmentAddRideBinding
return rideBiding.root
}
}