Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Android SmartFragmentStatePagerAdapter that intelligently caches the Fragments in the ViewPager
/*
Extension of FragmentStatePagerAdapter which intelligently caches
all active fragments and manages the fragment lifecycles.
Usage involves extending from SmartFragmentStatePagerAdapter as you would any other PagerAdapter.
*/
public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
// Sparse array to keep track of registered fragments in memory
private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Register the fragment when the item is instantiated
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
// Unregister when the item is inactive
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}

Cool have been looking for this

thank you very mutch... work like a charm

Nice!!!!!

I would make a little change and use generics so we don't need to cast when getting the fragment:

public abstract class SmartFragmentStatePagerAdapter<T extends Fragment> extends FragmentStatePagerAdapter {

    private SparseArray<T> registeredFragments = new SparseArray<T>();

    public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Register the fragment when the item is instantiated
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        T fragment = (T) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    // Unregister when the item is inactive
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    // Returns the fragment for the position (if instantiated)
    public T getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }
}

Now, when you extends this class, specify your fragment type:

public class MyPageAdapter extends SmartFragmentStatePagerAdapter<MyFragment> {
....

Nice, it help me a lot for understanding a cache for fragment

anshul90 commented Nov 6, 2015

Can you please give me a small example how I can manage my Fragments (with AsyncTasks) of ViewPager inside a Fragment by using this adapter?

This will lose its state on configuration change. The FragmentStatePagerAdapter will restore its cached fragment instances, and this object will have no registered instances.

f8full commented Apr 18, 2016

It helped so much ! 👍 👍

iRYO400 commented Jun 25, 2016

Is there anyway to refresh current fragment?

MahdiPishguy commented Jul 29, 2016

@gustavogianordoli

Hi thank you very much,
How can i use getRegisteredFragment method on SmartFragmentStatePagerAdapter deployed viewPager ? is it for check caching fragment?

@ghost

ghost commented Oct 21, 2016

firstly thanks you! hoping it is useful to me,

Owner

nesquena commented Oct 22, 2017

For those running into orientation change issues on rotation, I wanted to post a quick fix until we have a better workaround. In the SmartFragmentStatePagerAdapter class, extend with:

    // Add to `SmartFragmentStatePagerAdapter.java` as a new method
    // Returns the fragment for the position (if instantiated)
    public Fragment fetchFragmentByPosition(ViewGroup pagerInstance, int position) {
        Fragment existingInstance = registeredFragments.get(position);
        if (existingInstance != null) {
            return existingInstance;
         } else {
  	    return instantiateItem(pagerInstance, position);
         }
    }

In the parent activity or fragment where the adapter instance is available:

// In the Activity or wherever you have access to the view pager instance
adapter.fetchFragmentByPosition(mPager, 0);

This should allow you to get access to the instance even when the registeredFragments becomes null on orientation change. Can anyone else report back on if this code a) is working on orientation change b) isn't working on orientation change or c) proposed workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment