Updating the contents on tab changed in view pager

I am scracthing my head for past 1 day but unable to find the solution.
In mine application there are two tabs under the toolbar

  • First tab is USER-TAB

  • the second one is ADMIN-TAB

    In both the tabs there are the listView. When a ListItem on the USER-TAB is clicked a dialog appears and user take some action.

    Now after this when the ADMIN-TAB is Selected the Admin should get refreshed with new sets of data. But It’s not. On selecting the ADMIN-TAB the onResume() method and everyting is getting called but it is not able to update the list.

I wont be able to write the Whole code, I am giving some snippet.
Basically I have taken the code from this link

https://github.com/codepath/android_guides/wiki/Sliding-Tabs-with-PagerSlidingTabStrip

In My Main Activity I have written the OpPageChangeListener.

public class MaterialTab extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.material_main_sample);
        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager()));
        // Give the PagerSlidingTabStrip the ViewPager
        PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        // Attach the view pager to the tab strip
        tabsStrip.setViewPager(viewPager);
        tabsStrip.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    MileUserFragment userFragment = new MileUserFragment();
                    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.detach(userFragment);
                    ft.attach(userFragment);
                    ft.commit();
                } if(position == 1){
                    MileAdminFragment adminFragment = new MileAdminFragment();
                    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.detach(adminFragment);
                    ft.attach(adminFragment);
                    ft.commit();
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
}

OnPageSelected You can see I am detaching and reattaching the fragment.Everything is working fine. Both Fragments OnResume() are getting called but the List is not getting changed. I don’t undrstand why


Source: android

Leave a Reply