Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

android - ViewPager + FragmentStatePagerAdapter + orientation change

I have a little Problem: i have a ViewPager with some Pages and i use the FragmentStatePagerAdapter to handle the data. In portrait i have for example 20 pages for the ViewPager and in landscape i have just 10 pages for the ViewPager. So on a orientation change i create a new Adapter with different Data.

Here a little explanation why: I show one image in portrait and two in landscape but always all in count. If i have 10 pictures to show, i have 10 in portrait and 5 in landscape (always two).

But now i have a strange bug: When i am in Landscape on index 5 and turn the device, the ViewPager's current page is set to 10. If i turn it back again i am on page 5. If i swipe the ViewPage now to page 10 the methode getItem of the adapter is never called and the ViewPage shows me the one portrait picture and not the two for landscape. How can this happen? Is there a cache in the adapter or ViewPager? In the onCreate of the Activity everything is new created, the adapter and also the data (just strings with path) for the ViewPager. So any idea how to fix this really scary "feature"?

Here are some code:

onCreate:

    mViewPagerAdapter = new ReaderPageViewAdapter(getSupportFragmentManager(), getBaseContext(), mCurrentDocument.mPages, getResources()
                .getConfiguration().orientation);
    mPageReader = (ReaderViewPager) findViewById(R.id.pager);
    mPageReader.setAdapter(mViewPagerAdapter);

adapter getItem:

    public Fragment getItem(final int index) {
        final PageInfo pageInfo = mPages.get(index);
        final PageFragment pageFragment = (PageFragment) PageFragment
                .instantiate(mContext, pageInfo.mClss.getName(), pageInfo.mArgs);
        return pageFragment;
    }

If you need something more just tell me. Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can override the FragmentActivity onSaveInstanceState() and not call the super.onSaveInstanceState() in the method.

@Override
protected void onSaveInstanceState(final Bundle outState) {
    // super.onSaveInstanceState(outState);
}

Do not use mViewPager.setSaveEnabled(false);

it ended up with a big memoryLeak. After every change of the orientation it puts the Fragments into an array of FragmentManager and never cleaned it. So it goes up to over 100mb memory usage after change the orientation many times. Using the onSaveInstanceState method is a better way I think.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...