1

I have MainActivity with a ViewPager that hosts three tabs and their respective classes extend Fragment. One of the tabs has a layout with a ListView, the layout of the each item in the listView is as follows

TextView, checkBox and imageview

This ListView has customized adapter class that extends BaseAdapter.

What I want to do is, to save the check state of the Checkboxes of the ListView. in the class that extends Fragment I override the method onSaveInstanceState, the problem is the checkBoxes are defined in the getview() method in the class that extends BaseAdapter! I want to know how can I save the check state of the checkboxes in the method "onSaveInstanceState"?

reVerse
  • 35,075
  • 22
  • 89
  • 84
rmaik
  • 1,076
  • 3
  • 15
  • 48

1 Answers1

2

I think you might be misunderstanding the purpose of BaseAdapter. BaseAdapter-derived classes are meant to serve as an interface between a dataset and the AdapterView in which you display that dataset's information (and/or interact with it.)

With that in mind, I kind of think the easiest way to accomplish what you're doing is going to be to keep track of whether or not the item is checked using a boolean as part of the dataset. For example, if your dataset is an ArrayList<SomeObjectYouMadeUp>, you'll want to add a boolean member to SomeObjectYouMadeUp; set it during the CheckBox's OnCheckedChangeListener, and use it to determine whether the CheckBox should be visually checked during getView().

That might take some rethinking of your code, but trust me, you're probably going to want to do it. It's possible to do it the way you are describing, but it won't be easy or reliable; you'll want to get individual access to each visible view in your AdapterView using the method described here, but again I have to recommend against this.

The biggest reason is that onSaveInstanceState occurs most commonly during configuration changes - for example, a screen rotation. This means it is almost certainly an incorrect assumption that you'll be displaying the same Views. Say there are 10 Views visible in portrait orientation, and 5 visible in landscape. So the user rotates to landscape - which 5 do we get? Do you know for sure? You'll have to check each new View against some identifying information you probably also had to keep in onSaveInstanceState - and what if one of the new Views wasn't visible before the rotation?

The list of questions goes on. Do yourself a favor: save the check state with the rest of your dataset, and let Android figure it out for you.

Community
  • 1
  • 1
sigmabeta
  • 1,174
  • 1
  • 12
  • 28
  • actually i am working on an App with a fixed orientation, and i have a question, does saveinstancesate save the states of the views i am concerned about and helps returning them to the last set state even if i closed the App and restarted it? i wish my question is clear – rmaik Jan 29 '15 at 16:13
  • Your question is clear, but that is not the purpose of `onSaveInstanceState()`. You cannot use it to persist data permanently; there are a number of situations in which Android will destroy an Activity and recreate it because of a configuration change (screen rotation is one example, but there are many others, like plugging the device into a car dock). In these situations, `onSaveInstanceState()` is used to supply data to the recreated Activity to make the transition as seamless as possible. – sigmabeta Jan 29 '15 at 16:22
  • You can find more details about why you would use `onSaveInstanceState()` [here](http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState). – sigmabeta Jan 29 '15 at 16:23