Let's say I have two fragments A,B,C. Fragment A contains fragments B,C with a View Pager the allows to switch between the two fragments by using tab layout. Fragments B,C were created by using data from fragment A, Therefore I could pass a Listener from fragment A to fragments B and C and Receive events In those fragments in fragment A. However I want to receive events in fragments B,C That occurred on Fragment A (Clicking on an item of the toolbar of A).
How could I pass send An event from fragment A to fragments B,C in a similar way as the listener that was passed from fragment A To fragment B that allowed B to send events to A. If it is not possible with a listener it would be great to hear other option.
the solution to your problem is rather simple .Store the current fragment in a variable from Viewpager adapter getItem() method.
@NonNull
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragB=new FragB();
return fragB;
case 1:
fragC = new FragC();
return fragC;
}
}
Now when u wan to send data to fragment B and fragment C use this variables and send data to this fragments (Create method in fragment to handle the events). For example send event of button click.
view1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragB.onClick();
fragC.onClick();
}
});