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