Challenge: Play Sound Across Rotation

Currently, BeatBox will stop playing the sound if you rotate the device. This challenge will fix that.

The main issue in the current app is where you save your BeatBox object. Your MainActivity holds a reference to your BeatBox, but it is destroyed and re-created across rotation. This means that your initial BeatBox releases the SoundPool and re-creates it every time you rotate.

You have already seen how to persist information across rotation in GeoQuiz and CriminalIntent. Add a Jetpack ViewModel to BeatBox to keep your BeatBox object alive across rotation.

You can expose the BeatBox object as a public property from your ViewModel. This way, MainActivity can access the BeatBox instance from the Jetpack ViewModel to hand it to the data binding view model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.63.145