Edison
Edison

Reputation: 5971

FragmentPager not updating views

I tried to do what the other answers suggested, but when I look at the logs, after getItemPosition is returned with POSITION_NONE, position 0 does not get called for getItem, any idea? I also tried to override destoryItem when getItemPosition is returned, it DOES get called but nothing happens.

public class ProvidersActivityPagerAdapter extends FragmentPagerAdapter {

private ArrayList<String> fragments;

public ProvidersActivityPagerAdapter(FragmentManager fm) {
  super(fm);
  fragments=new ArrayList<String>();
  if(provider.isInitialized()){
    fragments.add(BaseDetailsFragment.class.getName());
  }else{
    fragments.add(BaseAuthFragment.class.getName());
  }
  this.fm=fm;
}

@Override
public int getCount(){
  return fragments.size();
}


@Override 
public int getItemPosition(Object object) {
  AppUtils.logI("Get Item Position: "+POSITION_NONE+" "+object.getClass().getName());
  return POSITION_NONE;
}

@Override
public Fragment getItem(int position) {
  AppUtils.logI("Get Fragment for: "+position);
  String f=fragments.get(position);
  if(f.equals(BaseDetailsFragment.class.getName())){
    return provider.getDetailFragment();
  }
  if(f.equals(BaseAuthFragment.class.getName())){
    return provider.getAuthFragment();
  }
  if(f.equals(BaseSelectorFragment.class.getName())){
    return provider.getCurrentSelector();
  }
  return new Fragment();
}

private void addItem(int loc,String fa,String tag){
  this.fragments.add(loc,fa);
  AppUtils.logI(fragments.toString());
  notifyDataSetChanged(); //only creates the new fragment but not updating the old ones. 
  mViewPager.setCurrentItem(loc);
}

Upvotes: 3

Views: 1014

Answers (1)

Edison
Edison

Reputation: 5971

It appears that the support library version of the FragmentPagerAdapter does NOTHING in destroy item. So the solution is to save the fragment manager when constructing the page adapter and override the destoryItem() function. Note that this may not be compatible to future editions of the support library.

@Override
public void destroyItem(ViewGroup container,int position,Object object){
  super.destroyItem(container, position, object);
  FragmentTransaction bt = fm.beginTransaction();
  bt.remove((Fragment)object);
  bt.commit();
}

Upvotes: 7

Related Questions