18

I have an activity that shouldn't be recreated after an orientation change. I added the following to the activity declaration in the manifest:

android:configChanges="orientation"

On Android 2.3 this works perfectly. On HONEYCOMB_MR2 onCreate is called anyway when change the orientation.

Is there something else that needs to be done on HONEYCOMB_MR2 to prevent recreating the activity after an orientation change?

hpique
  • 119,096
  • 131
  • 338
  • 476
  • Have you tried `android:configChanges="keyboardHidden|orientation"`? I wouldn't expect that to make a difference, but it's worth trying. – CommonsWare Aug 06 '11 at 16:32
  • @CommonsWare It does not, but trying all the possible values I found one that works. See my answer. – hpique Aug 06 '11 at 16:37

1 Answers1

38

Apparently using orientation|screenSize (?) prevents onCreate on Honeycomb and (so far) does not seem to break anything in previous Android versions.

android:configChanges="orientation|screenSize"

No idea why this is necessary and I don't really understand the documentation about this new value.

I suspect that in HONEYCOMB_MR2 upon an orientation change the activity considers this as two config changes: orientation and screen size. And both attempt to recreate the activity by default.

hpique
  • 119,096
  • 131
  • 338
  • 476
  • Yeah, those are new. What is your `android:targetSdkVersion` value? – CommonsWare Aug 06 '11 at 16:40
  • android:targetSdkVersion="13" – hpique Aug 06 '11 at 16:42
  • 2
    OK, well, that would trigger what the docs say. This has to do with those new resource set qualifiers: http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html Basically, since they expanded the former small/normal/large/xlarge and port/land stuff to include resource sets based on dimensions, they will consider changes in those dimensions to be configuration changes. Conceivably, there could be devices where those dimensions change not by orientation (e.g., dual-screen devices with second screen toggle-able on/off). – CommonsWare Aug 06 '11 at 16:50
  • Makes sense. Regrettably, Android's flexibility is making it really complicated. – hpique Aug 06 '11 at 16:57
  • I never promised you a rose garden. Which is a good thing, since I don't own a rose garden, and it's tough to buy those in stores. :-) – CommonsWare Aug 06 '11 at 16:59
  • Not having `screenSize` in my configChanges was causing `IllegalStateException`s stating that a fragment wasn't attached to an activity. I searched everywhere for what was causing this, since it was only happening on certain devices (3.2 and up, although I didn't realize that until I found this). Thanks! – ashughes May 23 '12 at 01:32
  • @CommonsWare sir i hve a fragment that gets added and hidden and paused on certain clicks. but when i change the locale, the fragment disappears. it works on 4.2.2 and 4.0 but not 3.2. i have the fragment getting called but it simply doesnt appear , what could be wrong ? please assist. – Rat-a-tat-a-tat Ratatouille Mar 06 '14 at 10:48