Preload fragment's view in ViewPager on first run - android

I'm using ViewPager that holds 3 fragments, each fragment loads a list. The problem is when the application runs for the first time and I swipe to the next fragment, this fragment needs sometime to load (about 2 seconds) before its view is visible. This is a very weird behavior. All I want is once the app has started, all fragments in ViewPager should be ready for user so when they swipe through fragments, there's no wait time. How can I do that?

Just call setOffscreenPageLimit() in onCreate() (after initializing ViewPager). The OffscreenPageLimit sets the number of pages that should be retained to either side of the current page (in your case 2). With this all of your fragments will be instantiate.
(An other (highly recommended) possibility is to increase the performance of your lists or listadapters, because a loading time of 2 seconds doesn't sound good)

Related

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

How to show fragment with data again when view is swiped back to in Viewpager having a tab layout?

I am trying to read RSS feed and show the content of it in card view which is inside ViewPager with tab layout. It shows fragment with data initially, but when swiped back from another tab, the whole card layout with data,disappears.
I had to handle the same situation. I had TabLayout in ViewPager.
I have used FragmentPagerAdapter with off screen page limit set to (NO_OF_TABS - 1). This is to hold all the fragments in memory and they wont be destroyed and recreated as you swipe back and forth. As my fragments are small in size meaning not many views every fragment has so it was okay for me. But this way fragments always holds the latest data. Say, if user had entered text in EditText and swipe to end and come back, then fragment will still have the text that user had entered.
In your case, if you have not set the page limit the default is 1 so the fragment gets destroyed when you swipe away so you lose the data.
IN ADDITION TO ABOVE:
If you can not keep all fragments in memory, you may consider using FragmentStatePagerAdapter. This adapter saves the states of fragments that are being destroyed and it will use the same state to represent in fragment when user swipe back to this fragment. But there are issues around StaePagerAdapter while it destroys adn recreate the fragments, there are indexing issues so you will easily get IndexOutOfBoundException if you swipe. This is with page screen limit set to less than the total fragments.

Why are the onStart and onResume methods for a Fragment called even though the fragment is not in the foreground

I am using a ViewPager inside a TabLayout. The ViewPager has 3 pages where each page is a fragment.
When the activity containing the TabLayout starts, the fragment callback methods upto onResume() are called for fragment1, which is as expected and fragment1 is displayed on the screen.
However, logcat shows that the callback methods upto onResume are also called for fragment2, although fragment2 is currently not being displayed on the screen. This looks odd to me since onResume should only be called when a fragment is about to become visible.
When I select fragment2, onResume is called for fragment3, although fragment3 is not being displayed. So there seems to be a pattern to this behaviour.
What could be the cause for this?
Update: The ViewPager I am using is a subclass of FragmentPagerAdapter.
If is default behavior of ViewPager to call next Fragment when ViewPager is initilized.
You need to use setOffscreenPageLimit()
setOffscreenPageLimit() sets the number of pages that should be retained to either side of the current page
Default value is 1 so next Fragment to the left and to the right will stay in memory.
To prevent from calling next Fragment you need to set 0 value to OffscreenPageLimit
viewPager.setOffscreenPageLimit(0);
on the ViewPager object.
EDIT:
But i have checked by setting limit to 0.
setOffscreenPageLimit(0) will not work now. when you set limit to 0 you can see below warning in LogCat:
Requested offscreen page limit 0 too small; defaulting to 1
So i advise you to either call setOffscreenPageLimit(2) which will keep all your 3 Fragments in memory otherwise don't call setOffscreenPageLimit(int limit) on ViewPager.
Two things contribute to this behavior:
Fragments are not required to have a UI, in which case it is never "visible" on screen. Basically, if a Fragment is attached to an Activity, its lifecycle methods will be called as appropriate.
ViewPager loads items to either side of the item that is currently displayed. This is in order to have content to show as the user scrolls the page with his finger. By default the off screen page limit is 1, which means that when your first fragment is the current item, the second fragment is loaded off screen in preparation for scrolling.
You can always check which item is displayed by the ViewPager using getCurrentItem(). If you are having problems because the fragment are destroyed and recreated needlessly when they go beyond the off screen page limit, you can increase the page limit with setOffscreenPageLimit().

CursorLoader with Fragment inside Fragment

I'm experiencing a really weird behavior and I suppose I'm missing something.
I have an activity that has a Fragment inside. This Fragment has a ViewPager which obviously hosts other Fragments.
To populate that ViewPager I need info from four tables. Once the first loader finishes loading (let's call it 0), I init the other loaders.
The user swipes through the pages and when he/she reaches the last page, the Fragment that is loaded on that last page inits loader 0 again, which takes a huge amount of time to reach onLoaderFinished. If I load that "last page fragment" without being inside the ViewPager, it loads instantly, as expected.
Is there a way to deal with this situation of a Fragment inside a ViewPager using the same Loader that was used to populate the ViewPager itself?
Thanks in advance.
Try calling setOffscreenPageLimit() in onCreate() (after initializing ViewPager). The OffscreenPageLimit sets the number of pages that should be retained to either side of the current page. With this all of your fragments will instantiate. Though you must not do so if there are large number of pages.
I solved the problem by not calling the Loader inside the Fragment and passing the array of data I wanted from the database in a Bundle as a Serializable.

Delay in ViewPager touch event

I'm using ViewPager in Activity, whose adapter is a subclass of FragmentStatePagerAdapter. I have only three pages and each page show a view generated by Fragment(not complicated view).
My issue is: every time when I load the viewpager, I have to wait nearly 3 seconds before I can swipe left/right. The time interval between onCreate and onResume is less than 0.3 second
My question is: what's the potential root causes for this kind of delay?
Thanks
If you could post some code that would be great, but if i was going to take a stab...
I would say that you have many items in the adapter, and you have set the setOffscreenPageLimit(int) too high, and the viewpager is loading the fragments of too many pages.

Categories

Resources