Retained Fragment upon pressing home


Hi all,

I am on chapter 14 coming across the case of retaining fragments upon rotation with setRetainInstance(true);

While this works and is a great fix for rotation changes, can one argue that it causes just as much problems when the user press home?

Specifically in this case, it is noticeable because as the user presses home while the audio is playing, the fragment is no longer on the screen but yet the audio is still running in the background.


I would like to dig deeper into this. Pressing the home button does not stop the audio from playing. As the audio clip is not too long this is not a big problem, but it could be in other circumstances. I developed a flashlight app and would like the light to remain on during rotations, but be turned off if the user leaves the app. Is there a way in the fragment to determine that the user has left the app so as to be able to release any resources? Or is there a different workaround?

BTW, love the book!