0

In my app I have the dropdown enabled in the ActionBar. The user has two elements to choose from. Depending on the choice I want a ViewPager to show different contents from different FragmentPagerAdapters. The user has to have the ability to switch all the time.

I've tried to set two different PagerAdapters in the listener with no luck. The Pager would just reload the previous fragments every time. Similar problems are described here and here. However in my approach I don't want to change the content of one adapter. I really just want to switch between two separate adapters.

This whole thing seems really confusing to me. Are there any known workaround for this or is there an alternative solution to my problem?

Community
  • 1
  • 1
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148

2 Answers2

1

If it is possible for your application, I would suggest using FragmentStatePagerAdapter instead of FragmentPagerAdapter. Each adapter should be able to store a separate list of fragments, and should keep the saved state of your fragments when you switch to the other adapter.

UgglyNoodle
  • 3,017
  • 1
  • 21
  • 17
0

I've tried to set two different PagerAdapters in the listener with no luck. The Pager would just reload the previous fragments every time.

I am assuming that you are using FragmentPagerAdapter or FragmentStatePagerAdapter. Both of those store their fragments by tag name in the FragmentManager, and therefore this switching approach will not work.

Are there any known workaround for this or is there an alternative solution to my problem?

One approach would be to fork FragmentPagerAdapter or FragmentStatePagerAdapter, replacing the makeFragmentName() method to use an alternative tag syntax, and use the revised adapter class for one of your two.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think you may be wrong about `FragmentStatePagerAdapter` storing fragments by tag names. Instead, it just keeps all active fragments in an `ArrayList`. – UgglyNoodle Oct 09 '12 at 03:00