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

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

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

way1: RecordSoundViewModel = hiltViewModel()

Way 2

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()
}

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

}

@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

way1: RecordSoundViewModel = hiltViewModel()

Way 2

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()
    }

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

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

}

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

}



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

答案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

override val value: VM
    get() {
        val viewModel = cached
        return if (viewModel == null) {
            val factory = factoryProducer()
            val store = storeProducer()
            ViewModelProvider(
                store,
                factory,
                extrasProducer()
            ).get(viewModelClass.java).also {
                cached = it
            }
        } else {
            viewModel
        }
    }

override fun isInitialized(): Boolean = cached != null

}

英文:

Yes you can use the same viewmodel instance.

It is understood when the source code is examined.

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

    override val value: VM
        get() {
            val viewModel = cached
            return if (viewModel == null) {
                val factory = factoryProducer()
                val store = storeProducer()
                ViewModelProvider(
                    store,
                    factory,
                    extrasProducer()
                ).get(viewModelClass.java).also {
                    cached = it
                }
            } else {
                viewModel
            }
        }

    override fun isInitialized(): Boolean = cached != null
}

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:

确定