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.
Related
I am having this problem with nested viewpagers.
I have two levels of viewpager. Lets call it levelOneViewPager inside which I have one more viewpager, let's call this levelTwoViewPager.
The Activity loads the data for levelOneViewPager and sets the adapter.
The levelOneViewPager loads data for levelTwoViewPager in onViewCreated and sets the adapter.
Everything works fine when the levelOneViewPager loads data for first time.
Now here is the problem.
First page of levelOneViewPager is loaded, I move to 4th page and come back to first page and the app crashes.
Reason, now first page of levelOneViewPager is recreated and is loading the data, even before it sets adapter, the fragments of levelTwoViewPager are created and is accessing some data in the parent fragment which is not available yet.
How is this happening? I haven't set the adapter to levelTwoViewPager yet but fragments are created for it.
(I tried using both FragmentPagerAdapter and FragmentStatePagerAdapter, it's the same behaviour)
add offscreenpage limit to parent viewpager this should fix the issue
i have an Activity which haves several Fragments and am using PagerAdapter for organising this Fragments , i got to know when my activity, loads all of my Fragment's codes also gets executed even when am at first Fragment at that time my whole codes of all fragment 2 or 3 also got executed which are in the onCreateView of my Fragment so can we stop this behaviour i want to execute this codes only when user selects that particular fragment
you can limit your pager by calling this method default load number is 3
yourPager.setOffscreenPageLimit([number of pages]);
if you want just one page without preload try this link by TejjD
I have an Activity with ViewPager and three Fragments with-in. One Fragment contains ListView with custom CursorAdapter which loads data from database.
I've noticed that my cursor adapter loads data every time I switch Fragments in ViewPager. I think that it's normal and is due to the fact that every Fragment has its own lifecycle.
Regarding this it will be great pleasure for me if the users of the stackoverflow explain about their experience or best practices at all.
Thanks!
ViewPagers maintain the state of only certain number of fragments, which defaults to 1 for both sides of the current selected fragment. For example, if you have 3 fragments, when the first is selected, only first two fragments will be instantiated and have the listview data loaded. Alternatively, if you have the second fragment selected, the first and the third will be instantiated. If you switch to the third fragment, the second fragment will be retained, but the first one will be lost. However, you can set the number of fragments to be retained with calling setOffscreenPageLimit method on viewPager with any number of fragments to retain you need. Though you should remember, that setting the number too high may cause your app to consume too much memory.
For example, if you want your fragment to not reload listview content from db while switching fragments and you have 3 framents in your viewPager, you may write the following code:
ViewPager mViewPager;
mViewPager.setOffscreenPageLimit(2);
I have an app that initially displays one fragment, when a button is clicked it transitions to the next fragment. However inside the 2nd fragment there's a ViewPager that holds images from a URL. It can take some time to process all the images. And it is processing the images as the button is clicked which then slows down the speed the fragment gets set up, which takes a lot of time. I want the fragment to be there and prepared instantly.
I can fetch the images in the mainActivity. I however cannot set them to the ViewPager before the fragmentTransaction is committed because the ViewPager doesn't exist until the onCreateView method occurs in the fragment which is only called when the transaction is committed. Is there any way to have the fragment set up in the activity before I commit the transaction.
So the user can easily switch between the two fragments without having to wait for them to process. Or should I have rather used tabs. The thing is I don't want a tabs bar to appear at the top of the screen. I want the tabs to change when by button is clicked.
It sounds like you're processing the images in the main thread? I think it would be best if, instead, you just pass the URL to your second fragment and have it use a Loader to process those images in the background, with placeholders for each image as it's getting loaded.
If you insist on doing the loading in the first fragment, you should just be able to set up and add your second fragment without any images, but have it be hidden. Then, once your loading is complete, you will still have access to your ViewPager from your first fragment and can use another transaction to show the second fragment when ready.
P.S. it would be easier to read/understand your question if it was laid out more logically and not a huge paragraph.
In my application I am using ViewPager from the support library- v4
In main screen I have viewPager which got max 5 Fragment, all fragment belongs to one class ArticlePager
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
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.
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?