How can I use Dependency Injection to pass Application() to ViewModel() using Hilt in Android Studio?
I 'm using Hilt as Dependency Injection, I hope to DI class UIApp into class SoundViewModel, so I use fun provideUIApp(): UIApp {...} as the following code.
I know Application() is a special class in Android , I don't know whether the way below is good, could you tell me ?
@HiltAndroidApp
class UIApp : Application() {
fun log(){
Log.e("my","Server")
}
}
@InstallIn(SingletonComponent::class)
@Module
object ObjectModule {
@Provides
@Singleton
fun provideUIApp(): UIApp {
return UIApp()
}
}
@HiltViewModel
class SoundViewModel @Inject constructor(
private val myapp: UIApp,
private val savedStateHandle: SavedStateHandle
): ViewModel() {
...
}
0 comments:
Post a Comment
Thanks