Fix saving fragments in TabsAdapter. Previously it didn't work when the activity was destroyed.

This commit is contained in:
Synced Synapse 2015-12-29 19:29:20 +00:00
parent fc079f5268
commit a6845c2e79
1 changed files with 8 additions and 4 deletions

View File

@ -64,16 +64,20 @@ public class TabsAdapter extends FragmentPagerAdapter {
return tabInfos.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = tabInfos.get(position);
return Fragment.instantiate(context, info.fragmentClass.getName(), info.args);
}
/**
* Store the created fragments, so that it is possible to get them by position later
*/
private HashMap<Integer, Fragment> createdFragments = new HashMap<>(5);
@Override
public Fragment getItem(int position) {
TabInfo info = tabInfos.get(position);
Fragment fragment = Fragment.instantiate(context, info.fragmentClass.getName(), info.args);
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment)super.instantiateItem(container, position);
createdFragments.put(position, fragment);
return fragment;
}