google-developer-training / basic-android-kotlin-compose-training-unscramble

Apache License 2.0
98 stars 140 forks source link

Android Basics: Viewmodel and State in Compose: Init is called over and over again #146

Closed amoh-godwin closed 11 months ago

amoh-godwin commented 12 months ago

When following the lesson on Viewmodel and State. The scrambled word keeps changing after I press inside the outlinedText and try to change the text. I decided to log the init and reliased that the init function gets called repeatedly. How do I stop it?

amoh-godwin commented 11 months ago

I have gotten it fixed. The problem was, I was doing gameViewModel: GameViewModel = GameviewModel() instead of gameViewModel: GameViewModel = viewModel() because the imports was showing an error. And the import was showing an error because I didn't have implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2") as part of the build.gradle.kts module version file. Now I have the dependency and so I have the correct code and so now it works