Instantly share code, notes, and snippets.

Embed
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);
}
}
@benkissi

This comment has been minimized.

Show comment
Hide comment
@benkissi

benkissi Jan 29, 2015

Cool have been looking for this

benkissi commented Jan 29, 2015

Cool have been looking for this

@rnascimento13

This comment has been minimized.

Show comment
Hide comment
@rnascimento13

rnascimento13 Jun 24, 2015

thank you very mutch... work like a charm

rnascimento13 commented Jun 24, 2015

thank you very mutch... work like a charm

@gustavogianordoli

This comment has been minimized.

Show comment
Hide comment
@gustavogianordoli

gustavogianordoli Oct 20, 2015

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> {
....

gustavogianordoli commented Oct 20, 2015

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> {
....
@kanglongba

This comment has been minimized.

Show comment
Hide comment
@kanglongba

kanglongba Oct 30, 2015

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

kanglongba commented Oct 30, 2015

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

@anshul90

This comment has been minimized.

Show comment
Hide comment
@anshul90

anshul90 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?

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?

@hashbrown

This comment has been minimized.

Show comment
Hide comment
@hashbrown

hashbrown Feb 20, 2016

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

hashbrown commented Feb 20, 2016

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

@asamoylenko

This comment has been minimized.

Show comment
Hide comment
@asamoylenko

asamoylenko commented Apr 7, 2016

@hashbrown yep!

@f8full

This comment has been minimized.

Show comment
Hide comment
@f8full

f8full Apr 18, 2016

It helped so much ! 👍 👍

f8full commented Apr 18, 2016

It helped so much ! 👍 👍

@iRYO400

This comment has been minimized.

Show comment
Hide comment
@iRYO400

iRYO400 Jun 25, 2016

Is there anyway to refresh current fragment?

iRYO400 commented Jun 25, 2016

Is there anyway to refresh current fragment?

@MahdiPishguy

This comment has been minimized.

Show comment
Hide comment
@MahdiPishguy

MahdiPishguy 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?

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

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Oct 21, 2016

firstly thanks you! hoping it is useful to me,

ghost commented Oct 21, 2016

firstly thanks you! hoping it is useful to me,

@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena 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

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

@kaushiknsanji

This comment has been minimized.

Show comment
Hide comment
@kaushiknsanji

kaushiknsanji Dec 4, 2017

@nesquena
Above code works great, just checked. One small correction to the above code, a type cast is required for the return statement -

return (Fragment)instantiateItem(pagerInstance, position); 

To simplify the class, I think we can have only one method instead of two doing the same work of getting the Registered Fragment -

// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
	Fragment existingInstance = registeredFragments.get(position);
	if (existingInstance != null) {
		return existingInstance;
	} else {
		return (Fragment) instantiateItem(pagerInstance, position);
	}
}

Wish I had this code earlier. I came across such a situation, and did not know what to do. Spent a lot of time, and finally made a hack from reading the internal code of FragmentStatePagerAdapter. Below is what I came up with, but your solution is very simple.

//Sparse Array to keep track of the registered fragments in memory
private SparseArray<Fragment> mRegisteredFragments = new SparseArray<>();

//Stores a reference to the FragmentManager used
private FragmentManager mFragmentManager;

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

/**
 * Overriding to restore the state of Registered Fragments array
 *
 * @param state  is the Parcelable state
 * @param loader is the ClassLoader required for restoring the state
 */
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
	super.restoreState(state, loader);
	if (state != null) {
		//When the state is present
		Bundle bundle = (Bundle) state;
		//Setting the ClassLoader passed, onto the Bundle
		bundle.setClassLoader(loader);

		//Retrieving the keys used in Bundle
		Set<String> keyStringSet = bundle.keySet();
		//Iterating over the Keys to find the Fragments
		for (String keyString : keyStringSet) {
			if (keyString.startsWith("f")) {
				//Fragment keys starts with 'f' followed by its position index
				int position = Integer.parseInt(keyString.substring(1));
				//Getting the Fragment from the Bundle using the Key through the FragmentManager
				Fragment fragment = mFragmentManager.getFragment(bundle, keyString);
				if (fragment != null) {
					//If Fragment is valid, then update the Sparse Array of Registered Fragments
					mRegisteredFragments.put(position, fragment);
				}
			}
		}
	}
}

kaushiknsanji commented Dec 4, 2017

@nesquena
Above code works great, just checked. One small correction to the above code, a type cast is required for the return statement -

return (Fragment)instantiateItem(pagerInstance, position); 

To simplify the class, I think we can have only one method instead of two doing the same work of getting the Registered Fragment -

// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
	Fragment existingInstance = registeredFragments.get(position);
	if (existingInstance != null) {
		return existingInstance;
	} else {
		return (Fragment) instantiateItem(pagerInstance, position);
	}
}

Wish I had this code earlier. I came across such a situation, and did not know what to do. Spent a lot of time, and finally made a hack from reading the internal code of FragmentStatePagerAdapter. Below is what I came up with, but your solution is very simple.

//Sparse Array to keep track of the registered fragments in memory
private SparseArray<Fragment> mRegisteredFragments = new SparseArray<>();

//Stores a reference to the FragmentManager used
private FragmentManager mFragmentManager;

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

/**
 * Overriding to restore the state of Registered Fragments array
 *
 * @param state  is the Parcelable state
 * @param loader is the ClassLoader required for restoring the state
 */
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
	super.restoreState(state, loader);
	if (state != null) {
		//When the state is present
		Bundle bundle = (Bundle) state;
		//Setting the ClassLoader passed, onto the Bundle
		bundle.setClassLoader(loader);

		//Retrieving the keys used in Bundle
		Set<String> keyStringSet = bundle.keySet();
		//Iterating over the Keys to find the Fragments
		for (String keyString : keyStringSet) {
			if (keyString.startsWith("f")) {
				//Fragment keys starts with 'f' followed by its position index
				int position = Integer.parseInt(keyString.substring(1));
				//Getting the Fragment from the Bundle using the Key through the FragmentManager
				Fragment fragment = mFragmentManager.getFragment(bundle, keyString);
				if (fragment != null) {
					//If Fragment is valid, then update the Sparse Array of Registered Fragments
					mRegisteredFragments.put(position, fragment);
				}
			}
		}
	}
}
@Coehill

This comment has been minimized.

Show comment
Hide comment
@Coehill

Coehill Jan 27, 2018

I'm getting FragmentStatePagerAdapter cannot be applied to (android.app.FragmentManager)
on the line:

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

Do you know what might be causing this?

EDIT:

I had to change:

import android.app.Fragment;
import android.app.FragmentManager;

to:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

Coehill commented Jan 27, 2018

I'm getting FragmentStatePagerAdapter cannot be applied to (android.app.FragmentManager)
on the line:

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

Do you know what might be causing this?

EDIT:

I had to change:

import android.app.Fragment;
import android.app.FragmentManager;

to:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
@q00u

This comment has been minimized.

Show comment
Hide comment
@q00u

q00u Aug 4, 2018

I'm still having trouble getting this to work after rotation. Is there an example project?

q00u commented Aug 4, 2018

I'm still having trouble getting this to work after rotation. Is there an example project?

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