How to avoid FragmentStatePagerAdapter overhead when switching between pages? - android

In my case I have 18 pages of a book. Some of the pages contain a lot of text data and the pager struggles in switching between these pages. Is there a workaround?

From the documantation:
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.
So maybe try to use the FragmentPagerAdapter instead.

Related

Prevent fragment re-create in viewpager

I want to prevent fragment re-create in viewpager , in default way every time you navigate to a fragment in viewpager , android creates a new instance of that fragment.
setOffscreenPageLimit(x) can fix this problem but i don't want to load more fragments in activity's onCreate. Any idea to solve this problem and maintain fragment instance after select for next time?
There are two most used classes for View Pagers are:
FragmentPagerAdapter
FragmentStatePagerAdapter
and they look alike in many ways but not all.
What you need is FragmentPagerAdapter
This will keep the Fragment as you want and not being recreated each time as the following quote say from the official documentantion:
Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.
And also here in the same documentation:
The fragment of each page the user visits will be kept in memory...
Although remember using this, memory will be your responsibility and you will have to manage memory of your Views check the number of pages you are having first if they are many, its not a nice idea to display 50 pages this way. Only may be less than 10 (Tabs may be its the best choice). You are good. Memory management is the only thing to take NOTE here as the documentation itself say:
This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider FragmentStatePagerAdapter.
So change your adapter to FragmentPagerAdapter and your fragments will be kept in memory not recreated.
Try setting Fragment.setRetainInstance(true). If you use FragmentPagerAdapter, this will cause an existing fragment to 'live' and not get re-created. It will to get detached and re-attached instead.
Try this, where you set the view pager adapter
viewPager.setOffscreenPageLimit(numberOfFragments)

Why should I use FragmentPagerAdapter instead of FragmentStatePagerAdapter?

I've been searching for it and all I found was the difference between them. And that's not the point.
If you use FragmentStatePagerAdapter in a ViewPager, you'll end up doing the same as you'd do with FragmentPagerAdapter, but consuming much less memory. If that's true, why should I use the FragmentPagerAdapter?
What is the advantages of using FragmentPagerAdapter?
What is the advantages of using FragmentPagerAdapter?
Speed, particularly when you have an intermediate number of pages: enough to easily hold in memory but beyond the handful that ViewPager wants to hold onto itself. With FragmentStatePagerAdapter, as the user navigates the pager, the adapter destroys some fragments and creates new ones. That takes time, both in terms of the direct Java code and in terms of the impact upon garbage collection. If you do not need that in some circumstance, why pay the price?
FragmentStatePagerAdapter:
If your page contains more fragments better to use FragmentStatePagerAdapter because it will save only state
of the fragment.
FragmentPagerAdapter:
Where as FragmentPagerAdapter will keep each fragment in memory as a result it will consume more moemory.
For example if you have around 3 fragments[in Viewpager] which contains less images/bitmaps better to go with FragmentPagerAdapter
For optimisation better to define mViewPager.setOffscreenPageLimit(2);
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state
if your structure depend on nested fragment and if you need to use childFragmentManager on inner fragment you might have a stack problem with FragmentStatePagerAdapter. when i got this problem i've changed FragmentStatePagerAdapter with FragmentPagerAdapter and it worked well.

Viewpager called multiple times

android View pager load two pages at a time into memory.i want to load one page at a time and call when page scrolled is it possible?
how can i load one page at a time when user scrolled then call second fragment?
actually it is 3. view pager always load 3 pages or to be exact 3 fragments into memory. this insures user doesn't encounter a frame drop. but there is a catch:
we have two kind of view pager adapter: FragmentPagerAdapter and FragmentStatePagerAdapter .
if you have fragments with heavily content i suggest FragmentStatePagerAdapter because it doesn't load all 3 fragments together. it loads the first one and the save only the states of the other two. its better in case you want to manage memory for heavily content Fragments

android -Need some suggestion for making a viewpager for very big datas

I want to make a leitner application . the data is coming from database .
For instance , I've about 500 rows in my database and I want to make a viewpager for showing them , each row comes on one page so I'll have 500 pages in the viewPager .
The question is , What is the best way to implement something like this ? is it going to have memory problems or something ?
What are the better ways to do so ? if no other way ,What type of viewPager is better to use for database? A fragment viewAdapter for example
For this you should use a normal ViewPager with FragmentStatePagerAdapter.
As described in documentation
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.

Difference between FragmentPagerAdapter with ViewPager with OffScreenLimit set to 1 and FragmentStatePagerAdapter?

What is the difference between FragmentPagerAdapter with ViewPager with OffScreenLimit set to 1 and FragmentStatePagerAdapter?
About FragmentPagerAdapter Google's guide says:
This version of the pager is best for use when there are a handful of
typically more static fragments to be paged through, such as a set of
tabs. The fragment of each page the user visits will be kept in
memory, though its view hierarchy may be destroyed when not visible.
This can result in using a significant amount of memory since fragment
instances can hold on to an arbitrary amount of state. For larger sets
of pages, consider FragmentStatePagerAdapter.
And about FragmentStatePagerAdapter:
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.
I think if I set the offscreenlimit to 1, it would destroy all fragments outside of the two next of the current fragment, and it behaves similar to a FragmentStatePagerAdapter. Is this correct?
The difference is exactly as it's written in docs. But it can be a little confusing.
FragmentPagerAdapter holds its fragments in the FragmentManager in detached state while they are not visible (while they are over the offscreen limit bounds) and FragmentStatePagerAdapter removes them from the FragmentManager.
The offscreenPageLimit is something little different. All fragments within it stays attached to the UI. Once they go over, they are either removed, or detached.
You can see it in sources of FragmentPagerAdapter (line 121) and FragmentStatePagerAdapter (line 144)

Categories

Resources