0

I have an Android Activity with a RelativeLayout and I have implemented the following method to prevent the activity from being recreated on change of Orientation:

@Override
public void onConfigurationChanged(Configuration newConfig) {       
    super.onConfigurationChanged(newConfig);

}

I am obviously not doing anything in this method, but it worked perfect when using a LinearLayout. Now however, using RelativeLayout, my layout is all messed up when changing to landscape orientation.

What is the most efficient way to have the screen redraw correctly without having the activity restarted again with a call to onCreate?

JohnRock
  • 6,795
  • 15
  • 52
  • 61

2 Answers2

2

You have two ways:

  • Retaining an Object During a Configuration Change
  • Handling the Configuration Change Yourself

You have chosen the second way. In this case you have to reassign every resource by hand and this approach is not recommended.

IMHO the best way is the first by implementing the method: onRetainNonConfigurationInstance();

Here a complete example on howto use.

Note: the onCreate will be called again but you can adjust your code to prevent long runnning task to be called again during a configuration change

Francesco Laurita
  • 23,434
  • 8
  • 55
  • 63
  • Thanks for this advice. I read the example you cite. So, it looks to me like using onRetainNonConfigurationInstance() does in fact kill the activity and restart it again but provides a way to cache any data that was loaded. That is great, but you still need to incur the cost of restarting the activity and reloading all the views... But I guess that is just the way it has to be done :) So if using onRetainNonConfigurationInstance() I imagine I should NOT set my activity as: android:configChanges="keyboardHidden|orientation". All I need to do is implement that method ? – JohnRock Jun 08 '10 at 11:49
  • You mention that using option 1 I must "reassign every resource by hand". Reassigning every resource then I take it means call methods like 'TextView myView = findViewById(R.id.myTextView); myView.setText("some content")' again for all displayed resources? – JohnRock Jun 08 '10 at 11:55
  • "I imagine I should NOT set my activity as: android:configChanges="keyboardHidden|orientation" Yes! For the second comment, I mean: if you provide alternative drawable resources for landscape and potrait mode by overriding onConfigurationChanged you have to assign this alternative resources by hand. You can pick the right poiter from the newConfig parameter which is up-to-date. For the RelativeLayout problem, did you try to invalidate it? – Francesco Laurita Jun 08 '10 at 12:28
  • "For the RelativeLayout problem, did you try to invalidate it?" What do you mean by this? – JohnRock Jun 09 '10 at 02:27
  • @johnrock I mean something like relativeLayout.invalidate() – Francesco Laurita Jun 10 '10 at 13:14
  • I think I have narrowed down the problem I am having to the fact that my RelativeLayout is wrapped by a ScrollView. Inside the RL there are three buttons set to alignParentBottom. When I change to landscape orientation the screen does not fit and the bottom has scrolled out of view - hence, the three buttons are now at the top of the screen. At the moment I am assuming that this may be because the parentBottom is not visible (thanks to the scroll view.) Does that seem right? Is there a problem with aligning to parent bottom when within a scrollview that scrolls off the screen? – JohnRock Jun 12 '10 at 14:49
  • 1
    please Dont post the link that expires in certain duration. – Pattabi Raman Nov 19 '12 at 11:59
2

There is a third and easier way that is missing in Francescos answer.

Just add

android:configChanges="keyboardHidden|orientation"

to the activity tag of every activity that you don't want to restart on a change from landscape to portrait mode. This will cause the OS to rebuild your layout without destroying it before the rebuild. The oncreate method will not get called again and you don't loose the state of the activity. But be carefull this will only work if you use the same layout file for portrait and landscape mode.

See this question for more information on this topic

Community
  • 1
  • 1
Janusz
  • 187,060
  • 113
  • 301
  • 369
  • In according with the official documentation, adding android:configChanges="keyboardHidden|orientation" is simply declare an Activity which will handle the configuration changes by itself. So IMHO it looks like just another declination of the my second option – Francesco Laurita Jun 07 '10 at 11:04
  • Yes that is correct sorry if you declare this and overwrite the onConfigurationChanged you have your second solution – Janusz Jun 07 '10 at 12:22
  • THis third way is in fact exactly the way I am doing it in my example. The problem is that it does successfully redraw the layout for LinearLayout, but not for RelativeLayout - at least not on the Nexus 1. – JohnRock Jun 08 '10 at 12:01