[android-developers] Preserving Compound Control View State Across Configuration Changes
I have a compound control containing an AutoCompleteTextView, a CheckBox, and an ImageButton. The layout is inflated from xml. The control is added added at runtime to a linearlayout when a user clicks an 'Add' button.
I need to preserve the values of the textview and the checkbox across configuration changes, so I implement the following INSIDE the compound control class.
@Override
protected Parcelable onSaveInstanceState() {
// Create a bundle to save our state in
Bundle b = new Bundle();
// Save the superclass' instanceState
b.putParcelable("instanceState", super.onSaveInstanceState());
// Save the AutoCompleteTextView
AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.houseEditFragmentProConView_autoCompleteTextView1);
b.putString("acTextView", acTextView.getText().toString());
// Save the CheckBox
CheckBox cb = (CheckBox) findViewById(R.id.houseEditFragmentProConView_checkBox1);
b.putBoolean("cb", cb.isChecked());
// Return the saved state
return b;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle b = (Bundle) state;
AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.houseEditFragmentProConView_autoCompleteTextView1);
acTextView.setText(b.getString("acTextView"));
CheckBox cb = (CheckBox) findViewById(R.id.houseEditFragmentProConView_checkBox1);
cb.setChecked(b.getBoolean("cb"));
super.onRestoreInstanceState(b.getParcelable("instanceState"));
return;
}
super.onRestoreInstanceState(state);
}
When I change the orientation of the screen, ALL of these controls are lost. So I overrode the fragment's onSaveInstanceState method with a loop to store all of the control.getid()'s in an ArrayList<Integer>. Then in the fragment's onViewCreated method, I loop through that arrayList (extracted from the bundle), and create a control for each id in the arraylist, and set that id on it before adding it to the containing view.
As I understand it, if you recreate a control and give it the same ID as a control that was destroyed in an orientation change, Android will refill the data it had. My problem is that even though all the controls show back up and have data in them, they all have the SAME data as the LAST control that was stored, instead of having their individual unique datas.
How can I fix this? Relevant portions from the fragment posted below:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LinearLayout prosConsView = (LinearLayout) getSherlockActivity()
.findViewById(R.id.houseEditFragment_linearLayout_Pros);
if (savedInstanceState == null) {
buttonAdd.performClick();
EditText editTextAddress = (EditText) getSherlockActivity()
.findViewById(R.id.houseEditFragment_editText_Address);
editTextAddress.requestFocus();
} else {
// TODO: Restore fragment from savedInstanceState in case of
// orientation
// change or if this gets killed
ArrayList<Integer> recoveredProsConsIds = savedInstanceState
.getIntegerArrayList("prosConsIds");
Log.d(LocalApplication.DEBUG_TAG, "RECOVERED ARRAY: "
+ recoveredProsConsIds);
HouseEditFragmentProConView workingProConView;
for (Integer id : recoveredProsConsIds) {
workingProConView = new HouseEditFragmentProConView(
getSherlockActivity(), null);
workingProConView.setId(id);
Log.d(LocalApplication.DEBUG_TAG, "Restore: "
+ workingProConView.getId() + " " + workingProConView.getText().toString());
prosConsView.addView(workingProConView);
}
for (int i = 0; i < prosConsView.getChildCount(); i++) {
workingProConView = (HouseEditFragmentProConView) prosConsView
.getChildAt(i);
Log.d(LocalApplication.DEBUG_TAG,
"Check: " + workingProConView.getId() + " " + workingProConView.getText().toString());
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Save the form in case of configuration change or if the user
// changes apps and this gets killed
// EditText address = (EditText)
// getSherlockActivity().findViewById(R.id.houseEditFragment_editText_Address);
// outState.putString("address", address.getText().toString());
LinearLayout prosCons = (LinearLayout) getSherlockActivity()
.findViewById(R.id.houseEditFragment_linearLayout_Pros);
ArrayList<Integer> storedProsConsIds = new ArrayList<Integer>();
for (int i = 0; i < prosCons.getChildCount(); i++) {
HouseEditFragmentProConView current = (HouseEditFragmentProConView) prosCons
.getChildAt(i);
storedProsConsIds.add(current.getId());
Log.d(LocalApplication.DEBUG_TAG, "Save: " + current.getId() + " " + current.getText().toString());
}
outState.putIntegerArrayList("prosConsIds", storedProsConsIds);
super.onSaveInstanceState(outState);
}
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home