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 different Activity or another Activity appears over this one, they press the back button, whatever.
I believe there's an API for this in Honeycomb, but I need something that will work in Android 2.2.
I'd rather not use onConfigurationChanged to handle all the configuration changes myself--that sounds like a lot of work and potential bugs--and the issue with onRetainNonConfigurationInstance() is that it doesn't run until after onStop fires--but onPause would be the logical place to pause the media, if appropriate.
Is there a way, in onPause, to tell if the Activity is being paused for a configuration change versus another reason?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With