New to DI, Using Hilt I'm trying to inject an interface implemented by an activty to a viewmodel like this:
Activity class:
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), AnInterface{
private val mainViewModel : MainViewModel by viewModels()
// .....
}
ViewModel:
@HiltViewModel
class MainViewModel : ViewModel() @Inject constructor(anInterface : AnInterface){
}
Module:
@Module
@InstallIn(ActivityComponent::class)
class ActivityModule {
@Provides
fun provideAnInterface(activity: Activity) : AnInterface = activty as AnInterface
}
Gives me error:
AnInterface cannot be provided without an @Provides-annotated method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.