Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewpager in fragment not getting updated when navigating back to fragment

Below is the code of my ViewpagerAdapter

private class ImagePagerAdapter extends FragmentStatePagerAdapter
{
    public ImagePagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();

        args.putInt(DemoObjectFragment.ARG_OBJECT, arg0 );
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub

        int returnVal=0;
        if (totalPlayer%5==0) {
            returnVal= totalPlayer/5;
        }
        else
        {
            moduloImage=totalPlayer%5;
            returnVal= (totalPlayer/5)+1;
        }   
        return returnVal;
    }

    @Override
    public int getItemPosition(Object object){
        return POSITION_NONE;
    }
}

When i load my fragment first time, it displays all items in view pager. Then when i navigate to new fragment by adding this fragment into stack, It shows next screen. After that when i pop previous fragment which conatins viewpager from stack on button back press, It displays all values as i had set before and also shows viewpager indicator to the exact position at which i had left but view pager items seems to be shown invisible. When i scroll viewpager, it is updated and shows perfect.

I have tried by updating viewpager but it seems to be doing nothing.

I have intialized view pager as,

pager.setAdapter(new ImagePagerAdapter(this.getFragmentManager()));
pager.setCurrentItem(0);
indicator.setViewPager(pager);
like image 551
Beena Avatar asked Nov 24 '14 04:11

Beena


People also ask

How do I refresh ViewPager fragments?

Using the ViewPager. OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager.

Is ViewPager deprecated?

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated. This method should be called by the application if the data backing this adapter has changed and associated views should update.


1 Answers

Finally I found the solution. Issue was in initialisation of my viewPager.

I need to initiate it by,

        pager.setAdapter(new ImagePagerAdapter(this.getChildFragmentManager()));

instead of

        pager.setAdapter(new ImagePagerAdapter(this.getFragmentManager()));

I had refered FragmentPagerAdapter getItem is not being triggered accepted answer.

Hope it might help someone who is facing the same issue.

like image 169
Beena Avatar answered Oct 26 '22 00:10

Beena