1

I have a ViewPager with 11 Fragments in it. I am looking for way to send arguments to the Fragment after they have been created

@Override
        public Fragment getItem(int position) {
            return new ScreenSlidePageFragment();
        }

This function is called only the first time. Each time i swipe the ViewPager and the Fragment is displayed to the user, i want to send new Arguments to the visible Fragment Fragment ? Is this possible ?

AndroidDev
  • 15,993
  • 29
  • 85
  • 119
  • 1
    Check out http://stackoverflow.com/questions/10078894/viewpager-pass-bundles-between-fragments – Fareya Nov 14 '14 at 19:52

1 Answers1

0

The first part of your problem is detecting when the page changes. You can set the ViewPager's OnPageChangeListener using pager.setOnPageChangeListener(). Within the OnPageChangeListener that you create, you'll want to override the onPageChanged method and put your logic into there. See this for reference: https://stackoverflow.com/a/11797936/3081611

The second part of your problem is how to send logic to the fragment itself. So you need some reference to the fragment. You can get the index of the fragment from the onPageChanged method. However, it doesn't actually return the fragment to you -- only the position. Getting the actual fragment is a little tricky. You can't use the pager's getItem function. It's a bit misldeading, but getItem() is what the ViewPager uses to create a new fragment. If you try to use it, you're just going to get the reference to a new ScreenSlidePageFragment that's not actually shown on screen.

A good way to get around this is to extend the ViewPager and make your own custom pager that overrides the instantiateItem() and destroyItem() functions so that when a fragment is created or destroyed, it gets stored in an array. Then you can make a function that returns the actual fragment from the array. See this for reference: https://stackoverflow.com/a/15261142/3081611

Community
  • 1
  • 1
Michael Marvick
  • 521
  • 4
  • 15
  • Fareya's response to your original question above addresses the 3rd part of the problem -- once you've detected that the page has changed and you have a reference to the fragment that's changed, how do you send data to it? Check it out as well if you need help with that part! – Michael Marvick Nov 14 '14 at 19:57