1

My Activity has a simple viewPager + viewPagerAdapter:

public class SearchFragmentAdapter extends FragmentPagerAdapter {

    // holds fragments placed in the viewPager
    private SparseArray<SearchFragment> mFragments;

    public SearchFragmentAdapter(Context context, FragmentManager fragmentManager, String queryText) {
        super(fragmentManager);

        mFragments = new SparseArray<SearchFragment>();
        List<Fragment> savedFragments = fragmentManager.getFragments();

        // try to restore fragments from fragmentManager
        if (savedFragments != null) {
            for (int i = 0; i < savedFragments.size(); i++) {
                if (savedFragments.get(i) instanceof SearchFragment) {
                    mFragments.put(i, (SearchFragment) savedFragments.get(i));
                }
            }
        }
    }

    @Override
    public Fragment getItem(int position) {
        SearchFragment fragment = mFragments.get(position);
        return (fragment == null) ? new SearchFragment() : fragment;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        // see if we already have this fragment
        SearchFragment fragment = mFragments.get(position);

        // if not => instantiate new fragment
        if (fragment == null) {
            final SearchFragment searchFragment = (SearchFragment) super.instantiateItem(container, position);
            mFragments.put(position, searchFragment);
            return searchFragment;
        }

        return fragment;
    }         
}

In every page of the viewPager is one SearchFragment. Every SearchFragment has only a ListView + listViewAdapter:

public class ListViewAdapter extends ArrayAdapter<Suggestion> {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // typical holder pattern
        if (convertView == null) {
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.row_suggestion, parent, false);
        }

        ...
    }
}

Every row of the listView has a button that start another Activity (YouTubePlayer, but that's not important). After the YouTube video is finished or the user clicks the back button, the YouTubePlayer-Activity closes and my own Activity + ViewPager + SearchFragments in ViewPages + ListViews in SearchFragments are shown again.

Although I can restore - the position of the viewPager (I save it in a field of the Activity) - the SearchFragments in the viewPager (see the constructor of SearchFragmentAdapter above)

I cannot restore the ListView and the ListViewAdapter which hold the content and the scrolling position of any of the ListViews. Of course, the same happens on configuration changes: The Activity is built again and can restore the position of the viewPager from its field, but the scrolling position and especially the content of the SearchFragments are lost.

How can I restore at least the content of the listViews (the scrolling position is just a nice-to-have) after the device was rotated?

I read a lot about state saving of viewPagers and ListViews, but there is hardly good information about state saving with this combination (viewPager + listView = 2 adapters with their own states and content).

muetzenflo
  • 5,653
  • 4
  • 41
  • 82

0 Answers0