I have a feeling that this is happening in the onRestoreInstanceState
method. Android automatically processes the state of most of the View
in your activity when it is paused and resumed. When resuming, it sounds like the checked state of your CheckBox
objects is being restored, starting onCheckedChanged
. Try this in your Activity
and let me know if this solves your problem:
private boolean mRestoringInstanceState; @Override protected void onRestoreInstanceState( Bundle savedInstanceState ) { mRestoringInstanceState = true; super.onRestoreInstanceState( savedInstanceState ); mRestoringInstanceState = false; }
Then change your onCheckChanged
method as follows:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(!mRestoringInstanceState) { Integer realPosition = (Integer) buttonView.getTag(); ListedPuzzle obj = items.get(realPosition); starListedPuzzle(obj.getId(), isChecked); } }
Edit: Probably a better / simpler solution would be to assign a listener in the onResume
method, since it will be called after onRestoreInstanceState
. Please note that when using this solution you will not implement any of the above:
@Override protected void onResume() { super.onResume(); CheckBox star = (CheckBox) v.findViewById(R.id.star_listed); star.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Edit2: Just realized that you are doing this in the Adapter
. This may not be related to onRestoreInstanceState
, but I think I see the problem. You reuse View
in the Adapter
, which is good, but you set star
to check before setting the listener. The problem is that star
already has a listener the last time it went through the getView
method. Before you call star.setChecked()
, call star.setOnCheckedChangedListener(null)
and find out if this solves your problem.
source share