6

Possible Duplicate:
Activity restart on rotation Android

I've got a LinearLayout that I want to be android:orientation="vertical" when in portrait mode, and android:orientation="horizontal" when in landscape. The activity performs some complex operations, so allowing the activity to close and restart itself on an orientation change is not an option, so that rules out using the layout-land feature or anything else involving restarting the activity. I am currently programmatically setting the correct android:orientation in activity's onStart and onConfigurationChanged.

Unfortunately, only about 20% of the time does it actually change the layout when you rotate the screen. I haven't been able to pick up on a pattern - it seems to do it sometimes, and other times it doesn't. I've tried calling .invalidate() on the LinearLayout after performing the change, but that didn't seem to work. How can I force the view to redraw with the new orientation, or there some better way to accomplish this?

Basically, here's the code in question that works sometimes, but not others:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    ((LinearLayout)contentView.findViewById(R.id.someLinearLayout))
        .setOrientation(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT ? 
                LinearLayout.VERTICAL
                : LinearLayout.HORIZONTAL);
}
Community
  • 1
  • 1
Keith
  • 2,958
  • 4
  • 26
  • 29
  • 2
    Your code should work 100% of the time. Without knowing what is in the layout, it will be difficult for anyone to help further. And unless the "complex operations" involve a `SurfaceView` or `WebView`, I am skeptical about your claims about the normal destroy-and-recreate mode being "not an option". – CommonsWare Apr 26 '11 at 23:26
  • Indeed, the LinearLayout in question contains Fragments which will contain WebViews. – Keith Apr 27 '11 at 03:08
  • Ah. Are you using native Honeycomb fragments, or the Android Compatibility Library? – CommonsWare Apr 27 '11 at 10:57
  • 1
    Beats me, then. Had you said you were using the ACL, I would have suggested trying the native HC stuff. But the ACL just passes through to the native HC stuff on Honeycomb, so switching to the ACL now is unlikely to help. Sorry! – CommonsWare Apr 27 '11 at 13:37
  • This doesn't exactly match up with what you're saying, but I had problems where *starting* my app in a particular orientation caused none of this code to fire and my layout to look goofy. – Chris Rae Feb 11 '14 at 23:41

0 Answers0