0

i am unable to reload/refresh Listfragment (CfFragment(),PfFragment(),IfFragment() in my below code) when tab is selected or swiped, can any one tell me where i am going wrong.My code is working well when it is created for the first time but it doesn't reload/refresh when swiped or selected.If i change anything in the 1st listfragment it should affect in the 2nd list fragment and so on... Please can any one help me out.it would be much appreciated. Thank you.

 public class busy extends FragmentActivity implements
        ActionBar.TabListener {


SessionManager session;

AppSectionsPagerAdapter mAppSectionsPagerAdapter;
static ServiceURL URL;

static AlertDialogManager alert = new AlertDialogManager();

ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    setContentView(R.layout.busy);      

    session = new SessionManager(getApplicationContext());              

    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {

                    actionBar.setSelectedNavigationItem(position);
                }
            });

    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
    }
}



@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {

    mViewPager.setCurrentItem(tab.getPosition());
    //mViewPager.setAdapter(mAppSectionsPagerAdapter);

}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

    public AppSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            return new CfFragment();
        case 1:
            return new PfFragment();
        case 2:
            return new IfFragment();

        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }

    public CharSequence getPageTitle(int position) {

        switch (position) {
        case 0:
            return "Confy";

        case 1:
            return "Peny";

        case 2:
            return "Incy";

        default:

            break;
        }
        return null;
    }
}

}

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
kiran guled
  • 97
  • 1
  • 2
  • 10

1 Answers1

3

Derive your AppSectionsPagerAdapter class as

public static class AppSectionsPagerAdapter extends FragmentStatePagerAdapter

instead of

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter

Basically, FragmentPagerAdapter keeps the created Fragments in memory, while FragmentStatePagerAdapter destroys and creates them anew as they are shifted in and out of view.

Two more things:

1. Make sure you are not calling setRetainInstance(true) in any of your Fragments, else they won't be recreated.

2. Add

viewPager.setOffscreenPageLimit(0);

to your code.

EDIT:

Instead of

Activity -> ViewPager -> Fragments

create the structure as

Activity -> Fragment -> ViewPager -> Nested Fragments

This will ensure that each Fragment is refreshed on tab change. See this post for the implementation.

Community
  • 1
  • 1
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • i have added as you said,but it is still remains the same...and i am not calling setRetainInstance(true) any where in that fragment...where am i going wrong? – kiran guled Mar 19 '15 at 12:26