[英]I can't inject fragment viewmodel with Hilt
I'm trying to inject the viewmodel to the Fragment using hilt, for that I created two modules one for my network component and another for the characters viewmodel.我正在尝试使用刀柄将视图模型注入片段,为此我创建了两个模块,一个用于我的网络组件,另一个用于角色视图模型。
The network module is installed in SingletonComponent and I need that the characters module installs in the FragmentComponent to get the Viewmodel through "by viewmodels()"网络模块安装在 SingletonComponent 中,我需要将字符模块安装在 FragmentComponent 中以通过“by viewmodels()”获取 Viewmodel
My activity and my fragment are annotated with "@AndroidEntryPoint" and my Application is annotated with "@HiltAndroidApp" my modules are:我的活动和我的片段用“@AndroidEntryPoint”注释,我的应用程序用“@HiltAndroidApp”注释我的模块是:
@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {
@Singleton
@Provides
fun provideLoggingInterceptor(): HttpLoggingInterceptor {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
return loggingInterceptor
}
@Singleton
@Provides
fun provideAuthorizationInterceptor(): AuthorizationInterceptor =
AuthorizationInterceptor()
@Singleton
@Provides
fun provideInterceptors(
loggingInterceptor: HttpLoggingInterceptor,
authorizationInterceptor: AuthorizationInterceptor
): HttpInterceptors = HttpInterceptors(
listOf(
loggingInterceptor,
authorizationInterceptor
)
)
@Singleton
@Provides
fun provideTimeouts(): HttpTimeouts = HttpTimeouts()
@Singleton
@Provides
fun provideHttpClient(
timeouts: HttpTimeouts,
interceptors: HttpInterceptors
): HttpBuilderFactory = HttpClientBuilderFactory(timeouts, interceptors)
@Singleton
@Provides
fun provideGsonConverter(): GsonFactory = GsonBuilderFactory()
@Singleton
@Provides
fun provideHttpServiceProvider(
httpBuilderFactory: HttpBuilderFactory,
gsonFactory: GsonFactory
): HttpProvider = HttpServiceProvider(httpBuilderFactory, gsonFactory)
}
@Module
@InstallIn(FragmentComponent::class)
class CharacterListModule {
@Singleton
@Provides
fun provideCharacterMapper(): Mapper<CharacterDataContainer, PaginatedCharacters> =
PaginatedCharactersMapper()
@Singleton
@Provides
fun provideCharacterServices(
httpServiceProvider: HttpProvider
): CharacterServices = httpServiceProvider.createService(CharacterServices::class.java)
@Singleton
@Provides
fun provideCharacterListRemoteSource(
characterServices: CharacterServices,
characterMapper: Mapper<CharacterDataContainer, PaginatedCharacters>
): CharacterListSource = CharacterListRemoteSourceImpl(
characterServices,
characterMapper
)
@Singleton
@Provides
fun provideCharacterListRepository(
characterListRemoteSource: CharacterListSource
): CharacterListRepository = CharacterListRepositoryImpl(
characterListRemoteSource
)
@Singleton
@Provides
fun provideCoroutineDispatcher() = Dispatchers.IO
@Singleton
@Provides
fun provideCharacterListUseCase(
coroutineDispatcher: CoroutineDispatcher,
characterListRepository: CharacterListRepository
) = CharacterListUseCase(
coroutineDispatcher,
characterListRepository
)
@Singleton
@Provides
fun provideCharacterUIMapper(): Mapper<Character, CharacterUI> = CharacterUIMapper()
}
and my viewmodel is:我的视图模型是:
@HiltViewModel
class CharacterListViewModel @Inject constructor(
private val characterListUseCase: CharacterListUseCase,
private val characterUIMapper: Mapper<Character, CharacterUI>
) : ViewModel() {
Also when I use SingletonComponent in both cases the application runs fine, but when try to use FragmentComponent fails:此外,当我在这两种情况下都使用 SingletonComponent 时,应用程序运行良好,但是当尝试使用 FragmentComponent 失败时:
Finally my dependencies are:最后我的依赖是:
object Hilt {
internal object Versions {
const val hilt = "2.33-beta"
const val hiltViewModel = "1.0.0-alpha01"
}
const val hilt = "com.google.dagger:hilt-android:${Versions.hilt}"
const val hiltCompiler = "com.google.dagger:hilt-android-compiler:${Versions.hilt}"
const val hiltViewModel = "androidx.hilt:hilt-lifecycle-viewmodel:${Versions.hiltViewModel}"
const val hiltViewModelCompiler = "androidx.hilt:hilt-compiler:${Versions.hiltViewModel}"
}
As it names says "FragmentComponent"(s) only live as long as it fragments does.正如它的名字所说,“FragmentComponent”(s)只存在于它的片段中。 You can't inject something that is fragment scoped into a view model, because viewmodels outlive its fragments lifecycle.. Change "FragmentScoped" with "SingletonScoped".
您不能将片段范围的内容注入到视图 model 中,因为视图模型的生命周期超过其片段生命周期。将“FragmentScoped”更改为“SingletonScoped”。
Please read the official documentation first before working with scopes.在使用范围之前,请先阅读官方文档。 99% of the time, using a "SingletonComponent" is more than enough
99% 的时间,使用“SingletonComponent”就足够了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.