Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell when a configuration change is happening in Froyo?

Tags:

android

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?

like image 542
Chad Schultz Avatar asked Nov 29 '11 17:11

Chad Schultz


1 Answers

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.

like image 69
benkc Avatar answered Nov 01 '22 23:11

benkc