Reputation: 141
I have a custom Viewpager in which it disables the touch events. So I have buttons 'Next' and 'Back' that control the viewpager. My question is how to pass data or bundles between the fragments in the viewpager. Normally it would work but sense the fragments are created even though they are not shown. That's because of the viewpager slide effect, it has to make the fragments ahead and before for the effect to work. So that means I can't use bundles since the fragment is already created. This is what I'm trying to do
Fragment 1 -> Fragment 2 -> Fragment 3
Fragment 1 is created and so is Fragment 2. When I press 'Next' Fragment 2 is shown. I want to pass a bundle to Fragment 3 when I press 'Next' again but Fragment 3 is already created so it won't work.
Another way I thought of is to call a method in each Fragment when the Viewpager sets it as its current Item.
Upvotes: 1
Views: 2922
Reputation: 30168
Why don't you create an interface that all of your fragments implement? This interface will have two methods: getParameterData()
and setParameterData()
. In your ViewPager, when they press next or previous, call getParameterData()
on the current fragment, then call setParameterData()
on the fragment to be displayed.
Upvotes: 1
Reputation: 35661
You could share/pass your objects between fragments by holding them in the host activity.
Upvotes: 1