Rpond, I have not overriden onResume, so I don't think that is the issue. Here is the Activity and associated layouts for everyone to see. In the code you'll see many Log.d statements (there were even more at one point.) With these Log statements I was able to verify that the code works exactly as I expect it to. Also, notice the onCheckChangedListener I add to each CheckBox. All it does is print a Log statement telling me the state of one of my CheckBoxes changed. It was through this that I was able to determine the state of the CheckBoxes were being altered after my onCreate returns. You'll see how I call examineCheckboxes() at the end of my onCreate. The Log statements produced from this are not what is shown on my screen after a rotation, and I can see the state of my boxes being altered afterwards (because of the onCheckChangedListener.)
SelectItems.java:
public class SelectItems extends Activity {
public static final int SUCCESS = 95485839;
public static final String ITEM_LIST = "item list";
public static final String ITEM_NAME = "item name";
// Save state constants
private final String SAVE_SELECTED = "save selected";
private DbHelper db;
private ArrayList<Long> selectedIDs;
ArrayList<CheckBox> cboxes = new ArrayList<CheckBox>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_items);
// Create database helper
db = new DbHelper(this);
db.open();
initViews(savedInstanceState);
examineCheckboxes();
}
private void initViews(Bundle savedState) {
initButtons();
initHeading();
initList(savedState);
}
private void initButtons() {
// Setup event for done button
Button doneButton = (Button) findViewById(R.id.done_button);
doneButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
done();
}
});
// Setup event for cancel button
Button cancelButton = (Button) findViewById(R.id.cancel_button);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
cancel();
}
});
}
private void initHeading() {
String itemName = getIntent().getExtras().getString(ITEM_NAME);
TextView headingField = (TextView) findViewById(R.id.heading_field);
if (itemName.equals("")) {
headingField.setText("No item name!");
} else {
headingField.setText("Item name: " + itemName);
}
}
private void initList(Bundle savedState) {
// Init selected id list
if (savedState != null && savedState.containsKey(SAVE_SELECTED)) {
long[] array = savedState.getLongArray(SAVE_SELECTED);
selectedIDs = new ArrayList<Long>();
for (long id : array) {
selectedIDs.add(id);
}
Log.d("initList", "restoring from saved state");
logIDList();
} else {
selectedIDs = (ArrayList<Long>) getIntent().getExtras().get(
ITEM_LIST);
Log.d("initList", "using database values");
logIDList();
}
// Begin building item list
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout scrollContent = (LinearLayout) findViewById(R.id.scroll_content);
Cursor cursor = db.getAllItems();
startManagingCursor(cursor);
cursor.moveToFirst();
// For each Item entry, create a list element
while (!cursor.isAfterLast()) {
View view = li.inflate(R.layout.item_element, null);
TextView name = (TextView) view.findViewById(R.id.item_name);
TextView id = (TextView) view.findViewById(R.id.item_id);
CheckBox cbox = (CheckBox) view.findViewById(R.id.checkbox);
name.setText(cursor.getString(cursor
.getColumnIndexOrThrow(DbHelper.COL_ITEM_NAME)));
final long itemID = cursor.getLong(cursor
.getColumnIndexOrThrow(DbHelper.COL_ID));
id.setText(String.valueOf(itemID));
// Set check box states based on selectedIDs array
if (selectedIDs.contains(itemID)) {
Log.d("set check state", "setting check to true for " + itemID);
cbox.setChecked(true);
} else {
Log.d("set check state", "setting check to false for " + itemID);
cbox.setChecked(false);
}
cbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("onClick", "id: " + itemID + ". button ref: "
+ ((CheckBox) v));
checkChanged(itemID);
}
});
//I implemented this listener just so I could see when my
//CheckBoxes were changing. Through this I was able to determine
//that my CheckBoxes were being altered outside my own code.
cbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
Log.d("check changed", "button: " + arg0 + " changed to: "
+ arg1);
}
});
cboxes.add(cbox);
scrollContent.addView(view);
cursor.moveToNext();
}
cursor.close();
examineCheckboxes();
}
private void done() {
Intent i = new Intent();
i.putExtra(ITEM_LIST, selectedIDs);
setResult(SUCCESS, i);
this.finish();
}
private void cancel() {
db.close();
finish();
}
private void checkChanged(long itemID) {
Log.d("checkChaged", "checkChanged for: "+itemID);
if (selectedIDs.contains(itemID)) {
selectedIDs.remove(itemID);
} else {
selectedIDs.add(itemID);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
long[] array = new long[selectedIDs.size()];
for (int i = 0; i < array.length; i++) {
array[i] = selectedIDs.get(i);
}
outState.putLongArray(SAVE_SELECTED, array);
}
//Debugging method used to see what is in selectedIDs at any point in time.
private void logIDList() {
String list = "";
for (long id : selectedIDs) {
list += id + " ";
}
Log.d("ID List", list);
}
//Debugging method used to check the state of all CheckBoxes.
private void examineCheckboxes(){
for(CheckBox cbox : cboxes){
Log.d("Check Cbox", "obj: "+cbox+" checked: "+cbox.isChecked());
}
}
}
select_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/heading_field"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginBottom="10dip" android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout android:id="@+id/button_layout"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true">
<Button android:id="@+id/done_button" android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Done" />
<Button android:id="@+id/cancel_button" android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
<ScrollView android:orientation="vertical"
android:layout_below="@id/heading_field" android:layout_above="@id/button_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:id="@+id/scroll_content"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
item_element.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_marginRight="5dip" />
<TextView android:id="@+id/item_name" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="18sp"
android:layout_centerVertical="true" android:layout_toLeftOf="@id/checkbox"
android:layout_alignParentLeft="true" />
<TextView android:id="@+id/item_id" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:visibility="invisible" />