1

Created a class which implements FragmentStatePagerAdapter and provided the function @Override public Fragment getItem(int position) in which I'm just creating and retuning fragment instance.

Then I've two functions, which is just changing the current page in ViewPager.

public void next() {
    this.mPager.setCurrentItem(this.mPager.getCurrentItem() + 1);
}

public void prev() {
    this.mPager.setCurrentItem(this.mPager.getCurrentItem() + 1);
}

So whenever the page is getting changed, I'm not getting call inside of any life-cycle functions, so that I can take any decision when the page is becoming visible, hidden etc...

Question

  1. How to properly implement ViewPager for Wizard things (for example registration or poll)

  2. Also I've read something like code below for properly changing/replacing the fragments. But how this can be used with ViewPager?

Please let me know

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.content_frame, fragment)
               .commit();
Vivek Kumar
  • 4,822
  • 8
  • 51
  • 85
  • Check this link https://dzone.com/articles/android-tutorial-using and cross check your view pager implementation – Vivek Mishra Mar 10 '16 at 07:33
  • check this http://developer.android.com/training/animation/screen-slide.html – Gaurav Mar 10 '16 at 07:54
  • @VivekMishra I checked the link you have shared, but still my questions are unanswered there. I have the working code, but I want to get the life-cycle call on when my views are getting replaced by ViewPager and FragmentManger. – Vivek Kumar Mar 10 '16 at 07:57
  • @GauravPolekar I had followed the same android tutorial which you have mentioned. But my question are after that. I will appreciate if you please read my question one more time. – Vivek Kumar Mar 10 '16 at 07:58
  • if you are manually sliding pages then I think pageChangeListener should work – Vivek Mishra Mar 10 '16 at 08:01

2 Answers2

0

FragmentStatePagerAdapter by default preloads adjacent fragment to that of showing fragment.

means if fragment 0 is showing fragment 1 also gets loaded with it. similarly if fragment 5 is showing then 4 and 6 will also be loaded with it.

hence when you slide from 4 to 5, 5's life cycle wont be called as it was already called.

you need to implement OnPageChangeListener to identify the change.

Ankit
  • 429
  • 3
  • 16
  • thanks for reply. Can you please tell me where to add the `OnPageChangeListener`. I guess on `ViewPager` class...??? – Vivek Kumar Mar 10 '16 at 08:33
  • Also when fragment 5 is shown after moving from 1 to 5, then fragments 1, 2, 3 OnDestroy() methods must be called? – Vivek Kumar Mar 10 '16 at 08:36
  • how to add listener -> http://stackoverflow.com/questions/11293300/determine-when-a-viewpager-changes-pages – Ankit Mar 10 '16 at 08:50
  • and for your second question, yes 1,2,3 fragment will be destroyed if offscreen page size is set to 1. if it is set to 2 then 2 additional fragment on either side of the current fragment will be loaded. and of course this applies to FragmentStatePagerAdapter. FragmentPagerAdapter will keep all its fragments in memory – Ankit Mar 10 '16 at 08:55
0

Try this:

In onCreate(),

pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
// Here you can specify how many pages(Fragments) to load.
pager.setOffscreenPageLimit(3);
pager.setPageMargin(pageMargin);

// Setting PageChangeListener...        
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {

    }

    @Override
    public void onPageSelected(int position)
    {
        // Here, you have a current selected page
    }

    @Override
    public void onPageScrollStateChanged(int state)
    {

    }
});    

// This method is used to get the current selected fragment from `ViewPager`
private YourFragment getCurrentFragment()
{
    YourFragment fragment = (YourFragment) adapter.getRegisteredFragment(pager.getCurrentItem());
    return fragment;
}

Now, FragmentStatePagerAdapter code

public class MyPagerAdapter extends FragmentStatePagerAdapter
{
    // Titles of the fragments
    private final String[] TITLES = { ... };
    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

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

    @Override
    public CharSequence getPageTitle(int position)
    {
        return TITLES[position];
    }

    @Override
    public int getCount()
    {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position)
    {
        if (position == 0)
        {
            return Fragment1.newInstance(position);
        }
        else if (position == 1)
        {
            return Fragment2.newInstance(position);
        }
        else if (position == 2)
        {
            return Fragment3.newInstance(position);
        }
        return Fragment4.newInstance(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)
    {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    public Fragment getRegisteredFragment(int position)
    {
        return registeredFragments.get(position);
    }
}

Hope this will help you out.

Faraz
  • 2,144
  • 1
  • 18
  • 28