Chapter 20 Challenge 2

I am stuck on this challenge. Does anyone have any advice as to how to solve this? I can’t seem to figure out a way to not recreate the beatbox every time the screen rotates.I created a new class called BeatBoxViewModel that implements jetpack ViewModel but I still can’t get this challenge to work properly.

Following the ViewModel and ViewModelFactory paradigm

this works when creating a Jetpack ViewModel. You’ll want to set up the following classes:

BeatBoxViewModel that accepts an AssetManager as a constructor argument. The class then has a public instance of BeatBox. Override the onCleared() method to release the sound pool.

BeatBoxViewModelFactory that accepts an AssetManager as a constructor argument. Override the create() method to create an instance of BeatBoxViewModel.

Now in MainActivity, create the factory and beatBoxViewModel objects. Anywhere that beatBox was referenced now replace it with beatBoxViewModel.beatBox. We will want to remove the onDestroy() overridden method to not release the sound pool when the activity is destroyed.

3 Likes

Thanks for the response. I was releasing SoundPool in the onDestroy()!

Thanks for the help.
Only the method create() don’t need override keyword otherwise I would presume the Factory Class would extend another with that method, but is not the case.