In FragmentPageradapter
I have 3 fragments. In first two fragments there is a SeekBar
. When either of the SeekBar
changes its value in fragment1
, I want to change the value ofSeekBar
which is inside fragment2
. How can I achieve It?

- 6,628
- 2
- 35
- 56

- 70
- 6
3 Answers
Solution as follows :
First create an interface
public interface SeekChangeInterface { public void sendChangedValue(int value) }
Implement it your Activity where the
FragmentPagerAdapter
BelongsCreate a object of interface in first fragment
SeekChangeInterface mSeekChangeInterface;
Declare
onAttach()
in the first fragment@Override public void onAttach(Activity activity) { super.onAttach(activity); mSeekChangeInterface = (SeekChangeInterface )activity; }
In seekbar change lisener call
mSeekChangeInterface.sendChangedValue(valueofseekbar)
Create a method in second fragment
public void setSeekBarValue(int progress){ seekBar.setProgress(progress); }
Call this method from the Activity
sendChangedValue()
public void sendChangedValue(int progress){ objectoffragnentpageradapter.getItem(1).setSeekBarValue(progress); }
Hope this will help you

- 8,595
- 7
- 42
- 71

- 2,446
- 5
- 29
- 53
Do not hold a reference to the fragment and try to update it this way. The fragment may be null, or not attached yet.
Try to use the Activity. Implement an setter in your activity and use it in the seekbar change listener to set the current value. In the other fragment use the activity to restore the value.
Something like:
((MyActivity) getActivity()).setSeekValue(40);
((MyActivity) getActivity()).getSeekValue();
note that this is not a perfect solution as the activity can be null too (add null checks) and you have to cast to MyActivity. But i think it is a usable quick solution.
Hope this helps

- 756
- 8
- 13
-
But how can i notify this value in another fragment in Fragment pager adapter ? onresume is not called when the tabviewpager changes – user3458049 Mar 10 '15 at 11:07
-
see comment in Mahmoud Elmorabea's answer – Patrick Dorn Mar 10 '15 at 11:53
One way to go is have your activity be responsible for the communication, meaning that when the seekbar changes, the fragment should do something like this:
((YourActivity) getActivity()).notifySeekBarValueChanged(int);
And in your activity
private void notifySeekBarValueChanged(int value) {
MyFragment frag = getSupportFragmentManager().findFragmentById(int);
frag.seekBarValueChangedTo(value);
}
Another way to go is to use some sort of messaging mechanism between your fragments like EventBus or Otto

- 3,243
- 1
- 14
- 20
-
Maybe a combination of both would be usefull. if fragment gets created by the adapter the value can be restored in onResume via the activity. if the fragment already exists the eventbus propagates the new value. – Patrick Dorn Mar 10 '15 at 11:52
-
There is a way with EventBus to have a default value if exists when you register for the first time, but great suggestion :) – elmorabea Mar 10 '15 at 11:57
-