Challenge 2: Play Sound Across Rotation

i moved around the Web for a while to understand why I’m getting these weird errors in the model view like

viewmodel has no zero argument constructor

till i figured out that i should have a ModelViewFactory, I checked the solution that was suggested in this chapter and implemented it.

I created BeatBoxViewModel.kt:


import android.content.res.AssetManager
import androidx.lifecycle.ViewModel

class BeatBoxViewModel constructor(val assets: AssetManager): ViewModel() {

    var beatBox = BeatBox(assets)

    override fun onCleared() {


also BeatBoxFactoryModel.kt


import android.content.res.AssetManager
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class BeatBoxFactoryModel(val assets: AssetManager) : ViewModelProvider.Factory {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return modelClass.getConstructor(

then implemented it in MainActivity

class MainActivity : AppCompatActivity() {

   private lateinit var  beatBoxViewModel : BeatBoxViewModel

    override fun onCreate(savedInstanceState: Bundle?) {

       val factoryModel =  BeatBoxFactoryModel(assets)
        beatBoxViewModel = ViewModelProvider(this, factoryModel).get(

and removed the onDestroy from MainActivity.

Thank you @jpaone i spent hours looking around till i saw your instructions, I was so close to start using Hilt to solve this…