Skip to content

Instantly share code, notes, and snippets.

@thanhhh
Last active November 17, 2015 06:28
Show Gist options
  • Save thanhhh/5802460311bf48688717 to your computer and use it in GitHub Desktop.
Save thanhhh/5802460311bf48688717 to your computer and use it in GitHub Desktop.
How to update data of active fragment item
public static class MyAdapter extends FragmentStatePagerAdapter {
private SparseArray mPageReferenceMap
= new SparseArray();
//other code
@Override
public Object instantiateItem(ViewGroup viewGroup, int position) {
Object obj = super.instantiateItem(viewGroup, position);
//Add the reference when fragment has been create or restore
if (obj instanceof TestFragment) {
TestFragment f= (TestFragment)obj;
mPageReferenceMap.put(position, f);
}
return obj;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//Remove the reference when destroy it
mPageReferenceMap.remove(position);
super.destroyItem(container, position, object);
}
public TestFragment getFragment(int key) {
return mPageReferenceMap.get(key);
}
//other code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment