开发者

Android: dynamically creating controls and orientation change

开发者 https://www.devze.com 2022-12-18 07:55 出处:网络
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

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. How开发者_StackOverflow社区ever 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消