0

如何将 HiltViewModelFactory 注入到非 Android 组件中,如 Conductor 的 Controller.. 等。目前我有这段代码,但它不起作用,说不能提供 HiltViewModelFactory。

@EntryPoint
  @InstallIn(ActivityComponent::class)
  interface MyClassInterface {
    fun viewModelFactory(): HiltViewModelFactory
  }

  private val viewModelStore = ViewModelStore()

  private val viewModelFactory by lazy {
    EntryPoints.get(requireContext(), MyClassInterface::class.java).viewModelFactory()
  }
4

1 回答 1

0

你可能会这样做:

@Module
@InstallIn(ActivityComponent::class)
class HiltViewModelModule {
    @Provides fun viewModelFactory(activity: Activity): HiltViewModelProviderFactory = 
        (activity as AppCompatActivity).getDefaultViewModelProviderFactory() as HiltViewModelFactory
}

这应该在任何带有注释的 Activity 中正常工作@AndroidEntryPoint

于 2020-06-28T18:12:11.653 回答