0

I have an application which uses ViewPager. Inside the ViewPager I don't use Fragments, just inflated views (a RelativeLayout with some content). These views are pre-created and held in a List. I created simple PagerAdapter implementation which overrides methods getCount, isViewFromObject, instantiateItem and destroyItem.

When running on Android 2.x device it all runs fine and the method instantiateItem is called just once for each page that gets loaded as an offscreen page.

But when I run on ICS, the instantiateItem method gets called in a loop and the whole application stops responding. All I was able to realize was, that the instantiateItem method gets called as a result of a measurement. But I have no clue why on ICS the measurement gets called periodically in a loop and what can I do to stop it.

Here is a code snippet, but I believe there's nothing special on it:

    mAdapter = new PagerAdapter()
    {
        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            return arg0 == arg1;
        }

        @Override
        public int getCount()
        {
            return mPages.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            View v = mPages.get(position);
            boolean isChild = false;
            for(int idx = 0; idx < getChildCount(); ++idx)
            {
                if(getChildAt(idx) == v)
                {
                    isChild = true;
                    break;
                }
            }
            if(!isChild)
                addView(v);
            return v;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object item)
        {
            removeView((View)item);
        }

    };
    setAdapter(mAdapter);

This code gets place in an initialization method of a class which derives from ViewPager.

I would really appreciate any hint, because this issue is driving me crazy and makes my application unusable on ICS devices.

TZHX
  • 5,291
  • 15
  • 47
  • 56
d.aemon
  • 761
  • 1
  • 7
  • 20

1 Answers1

1

The answer is hidden in application localization - I override Application.onConfigurationChange and set new default Locale and update configuration with this new Locale.

This is a recommended approach for forcing the application's Locale, but on ICS it caused my activity containing ViewPager to be recreated regularly when switched to landscape orientation.

If you ever meet this situation I recommend to override Activity.onConfigurationChange instead of Application's method. For some reason it works fine on ICS.

d.aemon
  • 761
  • 1
  • 7
  • 20