the 'cleanest' way IMHO is via interfaces and registering listeners
create two interfaces:
public interface LocationListener{
public void onLocationAvailable(/* whatever data you want to pass */ );
}
public interface LocationListenersRegistry{
public void addLocationListener(LocationListener listener);
public void removeLocationListener(LocationListener listener);
}
then you make your activity implement LocationListenersRegistry
and your fragments implement LocationListener
on your activity you'll have an private ArrayList<LocationListener>
that you'll add and remove listeners as per add/remove methods. Every time your activity receives new data, it should process it and then pass to all the listeners on the array.
on the fragments you should onPause
and onResume
register and unregister themselves from the activity, something like that:
onResume(){
super.onResume();
((LocationListenersRegistry)getActivity()).addLocationListener(this);
}
onPause(){
super.onPause();
((LocationListenersRegistry)getActivity()).removeLocationListener(this);
}
edit:
your activity implements the LocationListenersRegistry and then will have this code:
public class MyActivity extends Activity implements LocationListenersRegistry {
private ArrayList<LocationListener> listeners = new ArrayList<LocationListener>();
public void addLocationListener(LocationListener listener){
listeners.add(listener);
}
public void removeLocationListener(LocationListener listener){
listeners.remove(listener);
}
and then whenever the user clicks the menu button:
for(LocationListener l:listeners)
l.onLocationAvailable(/* pass here the data for the fragment */);
and your fragments will be implementing the LocationListener
public class MyFragment extends Fragments implements LocationListener{
public void onLocationAvailable( /* receive here the data */){
// do stuff with your data
}