[英]I can't inject fragment viewmodel with Hilt
我正在嘗試使用刀柄將視圖模型注入片段,為此我創建了兩個模塊,一個用於我的網絡組件,另一個用於角色視圖模型。
網絡模塊安裝在 SingletonComponent 中,我需要將字符模塊安裝在 FragmentComponent 中以通過“by viewmodels()”獲取 Viewmodel
我的活動和我的片段用“@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()
}
我的視圖模型是:
@HiltViewModel
class CharacterListViewModel @Inject constructor(
private val characterListUseCase: CharacterListUseCase,
private val characterUIMapper: Mapper<Character, CharacterUI>
) : ViewModel() {
此外,當我在這兩種情況下都使用 SingletonComponent 時,應用程序運行良好,但是當嘗試使用 FragmentComponent 失敗時:
最后我的依賴是:
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}"
}
正如它的名字所說,“FragmentComponent”(s)只存在於它的片段中。 您不能將片段范圍的內容注入到視圖 model 中,因為視圖模型的生命周期超過其片段生命周期。將“FragmentScoped”更改為“SingletonScoped”。
在使用范圍之前,請先閱讀官方文檔。 99% 的時間,使用“SingletonComponent”就足夠了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.