5

I have a custom ListView where clicking an item fires a new Activity. Each row of the ListView has a CheckBox and a TextView.

This is the getView method from my custom ListView:

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.puzzles_row, null);
        }
        ListedPuzzle lp = items.get(position);
        if (lp != null) {
            TextView title = (TextView) v.findViewById(R.id.listTitles);
            title.setText(lp.getTitle());
            CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
            star.setChecked(lp.isStarred());
            star.setTag(new Integer(position));

            star.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    Integer realPosition = (Integer) buttonView.getTag();
                    ListedPuzzle obj = items.get(realPosition);
                    starListedPuzzle(obj.getId(), isChecked);
                }
            });

        }
        return v;

    }

The onCheckedChanged is called when coming back from the another Activty, without possible user interaction to actually check anything.

The point is that used to work fine and haven't really change anything. I've read that it's a bug but can't really believe it.

It always calls the method on the same two items of the ListView. Could they be set to checkedChange=true somehow?

Community
  • 1
  • 1
eskalera
  • 1,072
  • 2
  • 21
  • 36

4 Answers4

10

I have a feeling it is happening in the onRestoreInstanceState method. Android automatically handles restoring the state of most Views in your activity when it is paused and resumed. When being resumed, it sounds like it's restoring the checked state of your CheckBox objects, which is triggering onCheckedChanged. Try doing 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 like so:

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/easier solution would be to assign the listener in the onResume method instead since it gets called after onRestoreInstanceState. Note that you wouldn't implement any of the above if using this solution:

@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) {
            // onCheckedChanged implementation
        }
    });
}

Edit2: Just realized you're doing this in an Adapter. This may not be related to onRestoreInstanceState, but I think I see the problem. You are reusing Views in the Adapter, which is good, but you're setting star to checked before setting the listener. The problem is that star already has a listener from the last time it came through the getView method. Before you call star.setChecked(), call star.setOnCheckedChangedListener(null) and see if that solves your problem.

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • You are absolutely right! Now, though, clicking on my CheckBox doesn't do anything ofcourse. – eskalera May 18 '12 at 18:59
  • It should if you're setting `mRestoringInstanceState` back to `false` after `super.onRestoreInstanceState` finishes executing. – Jason Robinson May 18 '12 at 19:01
  • Additionally, you could also move the `star.setOnCheckedChangeListener...` line to the `onResume` method, as this gets called after `onRestoreInstanceState`. You wouldn't need to implement `onRestoreInstanceState` in this case. I'll edit my answer to show this option. – Jason Robinson May 18 '12 at 19:07
  • Well, it works like I said if I set `if(mRestoringInstanceState)`, but it doesnt work if I set `if(!mRestoringInstanceState)` – eskalera May 18 '12 at 19:07
  • @JasonRobinson: Wouldn't your `onResume` solution also require the listener to be set to `null` in `onPause`? – Squonk May 18 '12 at 19:14
  • @MisterSquonk No, I don't believe listeners are restored via `onRestoreInstanceState`. – Jason Robinson May 18 '12 at 19:18
  • @eskalera I just realized you're doing this in an `Adapter` and not an `Activity`. Makes my solution not worthwhile for you. Let me give you another solution. – Jason Robinson May 18 '12 at 19:22
  • @eskalera Sorry I'm throwing you around to different answers, but check my 2nd edit. – Jason Robinson May 18 '12 at 19:25
  • @Jason Robinson thanks, don't worry. I was just realising your 2nd solution can't be achieved since the `CheckBox` hasn't been initialised yet. – eskalera May 18 '12 at 19:27
  • @JasonRobinson, Working like a charm now! I'll keep dwelling on it for a while because didn't really get it yet. Thanks you so much. – eskalera May 18 '12 at 19:36
  • 3
    @eskalera The reason it works like that is that since you are reusing views in `getView`, `star` already has a listener attached to it from the last time that view was passed through `getView`. So when you checked `star`, it trigger the last listener that was assigned to it. It's up to you to uninitialize this before checking it. – Jason Robinson May 18 '12 at 19:40
9

It can be called on onRestoreInstanceState within your fragment

You may disable that with

android:saveEnabled="false"

on the Checkbox's layout definition

jmhostalet
  • 4,399
  • 4
  • 38
  • 47
  • This is the best solution for a really annoying problem. If onCheckedChanged() is being called unexpectedly try this. – Whome Oct 06 '17 at 01:01
6

I ran into this issue as well using a custom Adapter.

I found checking if the button isShown solves the problem:

OnCheckedChangeListener ccl = new OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

        if(buttonView.isShown())
        {

        }
    }
};
smetanma
  • 184
  • 3
  • 6
  • I think this is a fine solution as the system invoking the OnCheckedChangedListener when it is itself restoring state is obviously not what 99% of developers want. It is worth mentioning that ViewCompat.isLaidOut(buttonView) also works for this purpose and isn't tied strictly to visibility. – Stephen Kidson Jun 30 '16 at 01:16
1

Check your stacktrace in the listener

public void onCheckedChanged(CompoundButton buttonView, isChecked) {
    <breakpoint here>
}

If you see onRestoreInstanceState() in the stacktrace you can use this in the layout:

<CheckBox
...
android:saveEnabled="false" />

... as suggested here: Android CheckBox -- Restoring State After Screen Rotation

or

If you see that star.setChecked() is the one called from you getView() in the stacktrace then just remove the listener by star.setOnCheckedChangedListener(null) before star.setChecked() (then you can reset the listener as you do). Like Jason Robinson suggests.

Community
  • 1
  • 1
kotucz
  • 1,190
  • 13
  • 13