FragmentStatePagerAdapter performance issue - android

I have implemented an activity that displays fragments in a viewpager. The activity initially displays 5 fragments of a given type (type A). Each type A fragment contains a list of information. When a button is pressed, the fragments are replaced with another tpye of fragments, type B (i.e., fragments that use a different layout and display different information are loaded).
I have implemented this with a ViewPager and a custom FragmentStatePagerAdapter. The custom FragmentStatePagerAdapter overrides the getCount, instantiateItem, getItem, getItemPosition, etc. functions and all works fine. When I press the button the views switch from fragment type A to fragment type B. In order to do this I am using a replace fragment transaction and I also call notifyDataSetChanged on the adapter. The switch from type A to type B fragments is accomplished by checking which type of fragment I need to create in the adapter's getItem function. The problem is that the switching process (i.e., changing from fragment type A to fragment type B and vice versa) takes some time (around 2 seconds) - this delay is quite noticeable and annoying.
This occurs because the adapter's getItemPosition function returns POSITION_NONE. When POSITION_NONE is returned, the viewPager recreates the fragment. Furthermore, when the replace transaction is executed, all items of the adapter are removed and all new ones are recreated. The same happens when I switch back to fragments type A.
Does anyone know if there is a better or faster way to do this? Using a FragmentPagerAdapter should not be a good solution since the number of fragments in the viewPager could grow.
I have commented out the updating of the fragments and the problem is gone so the problem is the time it takes to update the fragments. Is there a way to update the fragments asynchronously? That should take care of the problem.

The ViewPager instatiates by default a minimun count of Fragments. In general the left and right one of the current Fragment. So you don't need to care about the total number of Fragments.
You can control the total number with the ViewPager.setOffScreenPageLimit() method.
Also a AsyncTask seems to be a good idea to manage the intensiv work of your Fragments.

Related

ViewPager Fragment RecycleView Reset

I have 3 fragments in the ViewPager.
PersonalFragment
CropFragment
NotesFragment
Every fragment has a RecyclerView and I was adding items using a dialog in it. However, when I go to the last fragment (i.e. NoteFragment), the first fragment's (i.e. PersonalFragment) RecyclerView gets empty (all items are removed). The same thing happens when I come back to the first fragment, the last fragment's RecyclerView resets (all items are removed).
It is difficult to understand without seeing some of your code which is related. I think you might have implemented the onResume function incorrectly. Each time you go to the third fragment in your ViewPager, the first Fragment needs to be reinitialized and you need to take care of this in your onResume function.
However, another workaround for your problem is setting the off-screen page limit of the ViewPager like the following.
viewPager.setOffscreenPageLimit(2);
Hope that helps.

Custom CursorAdapter loads data from db every time fragment switched

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);

Iterate over all currently loaded fragments in a ViewPager

I have a ViewPager. It has Fragments. When those Fragments are created, they load data from the server. When the user selects a menu item, I want to tell those fragments to filter their data without reloading from the server.
To do this, I have a subclass of FragmentPagerAdapter which keeps a List to hold a reference to each Fragment as it is created. When the menu item is selected, I simply iterate over that collection and call a method on each Fragment.
The problem arises when the device is rotated. The framework automatically restores the fragments to their parent view, without calling the getItem method of my adapter, meaning that my List is empty.
How can I either repopulate that List when the device is rotated, or obtain a list of all of the ViewPager's child Fragments without keeping a list?
EDIT: It may be important to note that my ViewPager lives in a Fragment, and that Fragment gets hot swapped via a side navigation menu. In other words, my ViewPager is not owned by an Activity, and there are some known issues about ViewPager not cleaning up its child fragments correctly which may be exacerbating the problem.
Check this link Get Current visible fragment in ViewPager
The author basically suggests 2 similar approaches which are to attach tags on fragment creation and keep track of it.
getSupportFragmentManager().beginTransaction().add(myFragment, "Some Tag").commit();
Then retrieve the tag like so:
int index = mViewPager.getCurrentItem();
String tag = mPageReferenceMap.get(index);
Fragment myFragment = getSupportFragmentManager().findFragmentByTag(tag);

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.

Recreate Fragment of - View Pager - FragmentPagerAdapter

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?

Categories

Resources