Will I get the same instance of ViewModel() when I use Hilt as DI in Android Studio project?

huangapple go评论73阅读模式
英文:

Will I get the same instance of ViewModel() when I use Hilt as DI in Android Studio project?

问题

I use Hilt as DI in Android Studio project, I know there are two ways to create the instance of ViewModel().

Way 1

  1. way1: RecordSoundViewModel = hiltViewModel()

Way 2

  1. val way2: RecordSoundViewModel by viewModels()

1: Coudl you tell me if viewModel1, viewModel2, viewModel3 and viewModel4 is the same instance of RecordSoundViewModel?

2: If the four instances are not the same instance of RecordSoundViewModel, how can I get the same instance?

@AndroidEntryPoint
class ActivityMain : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel3: RecordSoundViewModel by viewModels()
}

  1. override fun onNewIntent(intent: Intent?) {
  2. super.onNewIntent(intent)
  3. val viewModel4: RecordSoundViewModel by viewModels()
  4. }

}

@Composable
fun ScreenHome_Line(
viewModel1: RecordSoundViewModel = hiltViewModel()
) {

}

@Composable
fun ScreenHome_Recording(
viewModel2: RecordSoundViewModel = hiltViewModel()
) {

}

@HiltViewModel
class RecordSoundViewModel @Inject constructor(
...
): ViewModel()
{
...
}

英文:

I use Hilt as as DI in Android Studio project, I know there are two ways to create the instance of ViewModel().

Way 1

  1. way1: RecordSoundViewModel = hiltViewModel()

Way 2

  1. val way2: RecordSoundViewModel by viewModels()

1: Coudl you tell me if viewModel1, viewModel2, viewModel3 and viewModel4 is the same instance of RecordSoundViewModel?

2: If the four instances are not the same instance of RecordSoundViewModel, how can I get the same instance?

  1. @AndroidEntryPoint
  2. class ActivityMain : ComponentActivity() {
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. val viewModel3: RecordSoundViewModel by viewModels()
  6. }
  7. override fun onNewIntent(intent: Intent?) {
  8. super.onNewIntent(intent)
  9. val viewModel4: RecordSoundViewModel by viewModels()
  10. }
  11. }
  12. @Composable
  13. fun ScreenHome_Line(
  14. viewModel1: RecordSoundViewModel = hiltViewModel()
  15. ) {
  16. }
  17. @Composable
  18. fun ScreenHome_Recording(
  19. viewModel2: RecordSoundViewModel = hiltViewModel()
  20. ) {
  21. }
  22. @HiltViewModel
  23. class RecordSoundViewModel @Inject constructor(
  24. ...
  25. ): ViewModel()
  26. {
  27. ...
  28. }

答案1

得分: 1

Yes, you can use the same viewmodel instance.

It is understood when the source code is examined.

public class ViewModelLazy @JvmOverloads constructor(
private val viewModelClass: KClass,
private val storeProducer: () -> ViewModelStore,
private val factoryProducer: () -> ViewModelProvider.Factory,
private val extrasProducer: () -> CreationExtras = { CreationExtras.Empty }
) : Lazy {
private var cached: VM? = null

  1. override val value: VM
  2. get() {
  3. val viewModel = cached
  4. return if (viewModel == null) {
  5. val factory = factoryProducer()
  6. val store = storeProducer()
  7. ViewModelProvider(
  8. store,
  9. factory,
  10. extrasProducer()
  11. ).get(viewModelClass.java).also {
  12. cached = it
  13. }
  14. } else {
  15. viewModel
  16. }
  17. }
  18. override fun isInitialized(): Boolean = cached != null

}

英文:

Yes you can use the same viewmodel instance.

It is understood when the source code is examined.

  1. public class ViewModelLazy<VM : ViewModel> @JvmOverloads constructor(
  2. private val viewModelClass: KClass<VM>,
  3. private val storeProducer: () -> ViewModelStore,
  4. private val factoryProducer: () -> ViewModelProvider.Factory,
  5. private val extrasProducer: () -> CreationExtras = { CreationExtras.Empty }
  6. ) : Lazy<VM> {
  7. private var cached: VM? = null
  8. override val value: VM
  9. get() {
  10. val viewModel = cached
  11. return if (viewModel == null) {
  12. val factory = factoryProducer()
  13. val store = storeProducer()
  14. ViewModelProvider(
  15. store,
  16. factory,
  17. extrasProducer()
  18. ).get(viewModelClass.java).also {
  19. cached = it
  20. }
  21. } else {
  22. viewModel
  23. }
  24. }
  25. override fun isInitialized(): Boolean = cached != null
  26. }

huangapple
  • 本文由 发表于 2023年5月20日 14:15:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76293782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定