Last active
November 17, 2015 06:28
-
-
Save thanhhh/5802460311bf48688717 to your computer and use it in GitHub Desktop.
How to update data of active fragment item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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