Saving programmatically added Orientation change views?

I'm having trouble saving Views when changing orientation. So this is what happens to me. I have a class extending the HorizontalScrollView that creates a LinearLayout and a button in it constructor. Additional buttons are added to LinearLayout when the button is clicked. When the action begins, I install the action bar in this custom view, and everything works fine, adding and removing buttons from LinearLayout. But here is the problem. If the switch orientation, onCreate restarts, a new instance of my user view is created and installed in the action bar. So my custom view goes back to the beginning when I switch the orientation.

How to maintain a hierarchy of views in all orientation changes?

+4
source share
2 answers

When the activity of a change in orientation is destroyed and recreated.

In your manifest, add the following to your activity.

<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/app_name"> //Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. //add screenSize for api 13 and above. 

Now that one of these configurations changes, MyActivity does not restart. Instead, MyActivity receives an onConfigurationChanged () call. This method is passed to the Configuration object, which indicates the new device configuration.

http://developer.android.com/guide/topics/resources/runtime-changes.html

+2
source

The best way for this information is to add android: configChanges = "orientation" to your activity in the manifest, and this will prevent the possibility of killing and restarting that Android assumes you want. Honestly, I would suggest adding this to any activity that does not have another layout file for landscape and portrait mode.

-1
source

All Articles