tags:

views:

226

answers:

1

Hi,

Currently I am working on an Android application that is dynamically creating controls. Everytime a user would click a button a new EditText appears below the button and the user can interact with the EditText. However if the screen orientation changes, the EditText's that the user created also disappears.

Code sample of user creating a EditText: (located in a onClick(), p is basic layoutParamas, and layout is a LinearLayout located undearneath the button)

    EditText editText = new EditText(this);
    layout.addView(buttonView, p);

Wondering what would be the easiest way to save the layout when the screen orientation changes so I do not loose any of the controls, or am I dyanmically creating the controls wrong.

Thanks.

update: By overriding the onSaveInstanceState() I was able to save a object which contained a list of all the controls the user orignally added. Using this list I was able to create the controls in the onCreate().

Still curious if there is an easier way to accomplish this task, as I would have to refactor a lot of code to fully implement this method.

+3  A: 

Try adding this to your <activity> tag in your manifest file:

android:configChanges="orientation|keyboardHidden"

Then do this in the activity class:

@Override
public void onConfigurationChanged(final Configuration newConfig)
{
    // Ignore orientation change to keep activity from restarting
    super.onConfigurationChanged(newConfig);
}

If you do this your activity will just be reused on orientation change instead of destroyed/recreated. So you won't have to save the state of everything you have going on.

mbaird
WoW. Thanks so much. Works wonderful.
BigAlHt