1

I want to be able to change the layout when a device is re-orientated to landscape or portrait. For speed and resource purposes (plus other issues applicable to my app) I do NOT want my app to be destroyed and restarted. I have several objects which I wish to retain between orientation changes as there is no benefit from destroying and re-creating them! I simply just want to change the position of some buttons and TextViews so that they suit the current orientation. Easy right?

Well no it isn't. To achieve the above I included in the app Manifest the configChange option for orientation change. Then I've implemented the onConfigurationChanged() where I determine and apply the appropriate layout. Simple yes?

But now take the textview I have in my layout. How on earth, using this particular method of responding to orientation changes, do I put the same text in the previous textview to the new textview? No instance data is passed to onConfigurationChanged() method. Also for some of the buttons, they could be disabled or enabled... I need to know this after orienatation change.

If I let Android destroy and restart my activity it's first going to create unnecessary work. All I want is to just move a few buttons and textviews.. NOT restart the whole app. That's just ludicrous!

Can anyone help me achieve what need?

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97

1 Answers1

2

An easy way to maintain configuration-independent data is to make use of onRetainNonConfigurationInstance() and its companion method getLastNonConfigurationInstance(). Just return an object that contains all the data that you want to reuse when your activity is recreated.

In Honeycomb, or if you are using the Android compatibility package, you can just call Fragment.setRetainInstance(true) instead. See the docs.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I tried the onRetainNonConfigurationInstance() but unfortunately it's useful for data only. I need to maintain a particular object between orientation changes (it's an object that maintains a network connection). I destroy this object in onDestroy() (as this is necessary when the app exits) and consequently the object is also destroyed on orientation change. I suppose I need a way to determine if it's an app exit or an app reorientation. – D-Dᴙum Aug 19 '11 at 07:50
  • You could set a flag when onRetainNonConfigurationInstance is called, to indicate that the activity is being reoriented. – Ted Hopp Aug 19 '11 at 14:44