Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager + FragmentStatePagerAdapter: ViewPager stops working when screen orientation changes

i am using ActionBarSherlock (which is basically an extension of the Android Support Package).

What i'm trying to do is the following:

I have a FragmentActivity which hosts just a single ViewPager. This ViewPager has a FragmentStatePagerAdapter (because there will be many items in the future). But for now it is just loaded with 2 items for testing.

Everything is working just fine while i am in portrait orientation. But when i change so landscape orientation it switches back to the first item in the adapter (which is fine since everything is reloaded etc), but i am unable to swype to the next item. There is just nothing happening.

From debugging i can see that the Loader return the two items just fine. getItem(...) is also called with position 0 and 1. So basicall everything looks fine, except it isn't ;)

Btw: the same thing is happening vice versa when i start in landscape orienation and switch to portrait orientation.

Any ideas what might be wrong here?

Here is some of my code:

public class QuotesStatePagerAdapter extends FragmentStatePagerAdapter {

private List<Quote> mQuotes;

public QuotesStatePagerAdapter(FragmentManager fm, List<Quote> quotes) {
    super(fm);
    mQuotes = quotes;
}

@Override
public Fragment getItem(int position) {
    Bundle arguments = new Bundle();
    arguments.putSerializable("quote", mQuotes.get(position));
    QuoteFragment fragment = new QuoteFragment();
    fragment.setArguments(arguments);
    return fragment;
}

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

}

public QuotesFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public void updateOrdering(ORDERING newOrdering) {
    mOrdering = newOrdering;
    getLoaderManager().getLoader(0).startLoading();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.quotes, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
    mViewPager.setOnPageChangeListener(this);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getLoaderManager().initLoader(0, null, this);
}

@Override
public Loader<List<Quote>> onCreateLoader(int id, Bundle args) {
    return new QuotesLoader(getActivity(), mCategoryId);
}

@Override
public void onLoadFinished(Loader<List<Quote>> loader, List<Quote> data) {
    mQuotes = data;
    mViewPager.setAdapter(new QuotesStatePagerAdapter(
            getSupportFragmentManager(), mQuotes));
}
like image 224
Goddchen Avatar asked Feb 18 '12 13:02

Goddchen


1 Answers

android:configChanges="orientation" worked like a charm, but i saw that it's not recommended by android team (only last case resource)... See here http://developer.android.com/guide/topics/manifest/activity-element.html#config

I found out that putting: setRetainInstance(true); on the onCreateView of each fragment retained the instance do the trick. (please not that the onActivityCreated will be called again)

like image 167
Mario Santos Avatar answered Oct 20 '22 20:10

Mario Santos