A really handy method since API-level 11 is isChangingConfigurations(). When you need to make a decision about which objects to tear down, observers to unregister, etc., you really want to know if your Activity is restarting, going onto the back-stack, or finishing for good.

isFinishing() differentiates between going to the back-stack or one of the other two cases, but doesn't help us to figure out if we're finishing for good or coming right back following a configuration change, say.

At API-level 11 we got a new method to help address that - isChangingConfigurations(). This is great - in lifecycle methods (typically onPause) we can check to see why we're pausing and potentially leave some of our long lived objects alone, being careful to avoid memory leaks, of course!

What options do we have prior to API-level 11? Not a whole lot, actually. The best I could come up with was to create a base Activity class (sub-classing FragmentActivity, obviously) and override two methods:

  1. onSaveInstanceState - overridden to set a boolean property isConfigChange to true.
  2. isChangingConfigurations - overridden to either invoke the super-class method or return the value of isConfigChange, depending on the API level running the app.

There is one big downside - onSaveInstanceState is not invoked until after onPause has completed, so isChangingConfigurations() will only return a correct value when invoked from onStop pre API-level 11.

Full source code below.

blog comments powered by Disqus