Nixit Patel
Nixit Patel

Reputation: 4445

Recreate Fragment of - View Pager - FragmentPagerAdapter

In my application I am using ViewPager from the support library- v4

  1. In main screen I have viewPager which got max 5 Fragment, all fragment belongs to one class ArticlePager

  2. Now in main screen there are list on categories, now the content of the pager is based on that selection,

The problem I am having is, I have used FragmentPagerAdapterwhich stores the Fragment and if the fragment is already exist, It will return the old Fragment without recreating it. That things runs perfectly, but Problem Occurs @ the time of orientation change.

For instance

If there are 5 View normally in every fragment For the given position, but There are also some which contains 2-3 views. Now if I change the orientation on page No. lets say 5 which contains only 3 view inside.

So, by now in every category on Page 5 I'll got the view containing 3 view, which is not something I want.

In my application each category contains the pagination

Is there any way such that i can destroy and recreate the Fragment on click of category? or any other work around

Thank you

Upvotes: 2

Views: 7029

Answers (2)

Nixit Patel
Nixit Patel

Reputation: 4445

OK thanks to open source I find my solution, FragmentPagerAdapter I have override the method instantiateItem and got the solution.

This can be easily achieved by FragmentStatePagerAdapter such that it doesn't store the fragment. It recreate it all the time, but I don't want that in 100's of page because of only few pages.

Upvotes: 6

Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

So if I understand correctly, your problem is that after rotation, the wrong set of fragments are in you ViewPager?

Why don't you check the current selected tab in onResume() or onStart() of your Activity, and create/assign a new PagerAdapter for you ViewPager with the correct fragments?

Upvotes: 0

Related Questions