In my app, I want a media file to play, and to keep playing if the user rotates the screen (destroying the Activity), but I want it to stop playing if the user moves to a differ
Prior to Honeycomb, onRetainNonConfigurationInstance()
as mentioned in the accepted answer is the best way to do this.
Starting with Honeycomb, that's deprecated, but there is a much simpler way: call isChangingConfigurations()
instead, during any of your onPause()
/onStop()
/onDestroy()
.
Finally, if using the support library FragmentActivity (
android.support.v4.app.FragmentActivity) on pre-Honeycomb, onRetainNonConfigurationInstance()
is declared final, but you can override onRetainCustomNonConfigurationInstance()
instead for the same effect.