Questions tagged [fragmentstatepageradapter]

Implementation of PagerAdapter class from the Android compatibility package that uses a Fragment to manage each page. This class also handles saving and restoring of fragment's state.

This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.


From the documentation of the FragmentStatePagerAdapter class:

Implementation of PagerAdapter that uses a Fragment to manage each page. This class also handles saving and restoring of fragment's state.

This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

Subclasses only need to implement getItem(int) and getCount() to have a working adapter.

Tag Usage:

397 questions
6
votes
4 answers

ViewPager with different adapters for portrait and landscape

In portrait mode, my ViewPager has 3 fragments A, B, C but in landscape mode, it has only 2 fragments A and C. So I create 2 FragmentStatePagerAdapters for each mode. The problem is when screen orientation changed, ViewPager restores and uses…
5
votes
0 answers

How to set custom tag for viewpager2 fragments?

How to set custom tag for viewpager2 fragments? How can i set a tag for viewpager fragments? The above question provides answers for ViewPager. But how can this be done for ViewPager2?
5
votes
2 answers

How can I use FragmentFactory with FragmentStatePagerAdapter

I like the approach of FragmentFactory, which makes it possible to pass dependencies to a fragment in a constructor. I'd like to use a custom FragmentFactory together with a FragmentStatePagerAdapter. I'm hosting the view pager in an activity. I can…
5
votes
0 answers

IllegalStateException: Fragment already added: FragmentPagerAdapter

I want to implement bottom navigation using view pager, but my app crashes with the below exception. Not able to find the exact issue here, can someone please help me in this ? java.lang.IllegalStateException: Fragment already added:…
5
votes
0 answers

TransactionTooLargeException with FragmentStatePagerAdapter

I wrote a reader app using ViewPager with offscreenPageLimit default to 1 and FragmentStatePagerAdapter, in which I have override fun getItem(position: Int) = ComicFragment.newInstance(index = position + 1) Here a ComicFragment is created with an…
5
votes
1 answer

Does FragmentStatePagerAdapter save fragment state on orientation change?

I have 3 fragments that need to be in a ViewPager. These fragment will hold dynamic information retrieved from a database. I understand that on an orientation change, the activity and fragments are destroyed and recreated. But I was under the…
5
votes
3 answers

Android - Child fragment not loading the second time inside FragmentStatePagerAdapter

I'm having an issue to display correctly fragments inside fragments with a FragmentStatePagerAdapter. The first time everything loads properly, but then the next time the view is blank. I tried this and this answers but it doesn't solve the…
Kalianey
  • 2,738
  • 6
  • 25
  • 43
5
votes
1 answer

Fragment already added IllegalStateException in viewpager

I'm using viewpager to display pictures. I just need three fragments basically: previous image to preview, current display image and next image to preview. I would like to just display a preview of previous and next image, it will change to full…
5
votes
2 answers

Changing background image of current fragment in viewpager

I have 4 fragments in a viewpager. And FragmentStatePagerAdapter inside the activity where my viewpager is. FragmentStatePagerAdapter's newInstance() method takes as parameter layout id, therefore each fragment has it's own layout: ViewPager…
5
votes
1 answer

FragmentStatePageAdapter: Multiple Items visible at the same time - How to center a specific page?

I'd like to use a FragmentStatePageAdapter and display 3 items at the same time. This could be done by overriding the getPageWidth()-Method: @Override public float getPageWidth(int position) { return 0.33f; } This works great.…
Frame91
  • 3,670
  • 8
  • 45
  • 89
5
votes
0 answers

Fragement no longer exists for key f0: index 1

I had a nice a pretty ViewPager/FragmentStatePagerAdapter working on my app but after manually adding a Fragment to the FragmentManager I started getting this exception when rotating the screen TWICE. StackTrace: 07-29 19:10:22.360:…
4
votes
0 answers

Androidx FragmentStatePagerAdapter - Cannot setMaxLifecycle for Fragment not attached to FragmentManager

Viewpager with FragmentStatePagerAdapter in androidx. When I try to replace a fragment on selected position (say pos 0) I get java.lang.IllegalArgumentException: Cannot setMaxLifecycle for Fragment not attached to…
4
votes
1 answer

Load same fragment with different data in ViewPager

I have 8 fragments with the same layout in my viewpager. The fragment contains a recycler view and a text view. My problem is that when i initiate the viewpager fragment then all the data gets loaded on the first fragment itself and nothing shows up…
4
votes
0 answers

ViewPager/RecyclerView initialization best practices

I am developing an android weather application that includes a Tablelayout with 3 pages. The adapter is a FragmentStatePagerAdaptater and in each of them is a Fragment that contains a RecyclerView. The application receives and processes the weather…
4
votes
0 answers

Saving RecyclerView Scroll position

I have an activity with a ViewPager and 3 fragments called A,B,C. A has a recyclerView populated by cardView and each card implements and OnClickListener which leads to a new Activity D. I want to be able to save the recyclerView scroll position…
1 2
3
26 27