The problem as follows. Let us have 3 tabs with fragments:
If you are moving from Fragment A to Fragment B, you can simply pass values to the constructor of fragment B.
Or else if you need the same set of data to be used in all three fragments, you can use share preferences as well.
I'd suggest to introduce EventBus in your app.
To add dependency - add compile 'de.greenrobot:eventbus:2.4.0'
into your list of dependencies.
Then, you just subscribe your third tab's fragment to listen to event from the first fragment.
Something like this: in Fragment B
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
eventBus.register(this);
}
@Override
public void onDetach() {
eventBus.unregister(this);
super.onDetach();
}
@SuppressWarnings("unused") // invoked by EventBus
public void onEventMainThread(NewDataEvent event) {
// Handle new data
}
NewDataEvent.java
public class NewDataEvent extends EventBase {
public NewDataEvent() {}
}
And in Fragment A just send the event:
protected EventBus eventBus;
....
eventBus = EventBus.getDefault();
....
eventBus.post(new NewDataEvent());
(and to avoid handling event in 2nd tab - just pass extra parameter during instantiation of fragment, if it has to listen to the event)
You can use the idea of Bundle for passing data between Fragments. I can see that my suggestion of this idea in a previous post was misunderstood. Here is sample code where the fragment receives the data. Fragment2:
public static Fragment2 newInstance(long param1, String param2) {
MediaFragment fragment = new Fragment2();
Bundle args = new Bundle();
args.putLong(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
Bundle bundle = getArguments();
audioId = bundle.getLong(ARG_PARAM1);
audioName = bundle.getString(ARG_PARAM2);
}
}
On another fragment or activity, calling Fragment2:
Fragment2 recvfragment = Fragment2.newInstance(itemId, itemName);
Notes:
Fragment2
.Are the fragments hosted in one activity? Then you could implement an interface on your hosting activity.
YourActivity implements MyInterface {
...
}
And in your fragments you define this:
@Override
public void onAttach(final Activity context) {
myInterface = (MyInterface) context;
}
And when you click something in your fragment then call myInterface.doSomething(parameter);
. And then your activity can delegate to another fragment.
You didn't write how are you adding fragments, during runtime or not. It will be better to add it during runtime and assign TAG to a each fragment, like this:
...
fragmentTransaction.replace(R.id.layoutTab2, fragment, "F-B-2");
...
fragmentTransaction.replace(R.id.layoutTab3, fragment, "F-B-3");
...
so later you can identify or find fragment by its tag. For example:
FragmentManager.findFragmentByTag("F-B-2")
or if you need fragment B 3:
FragmentManager.findFragmentByTag("F-B-3")
Hope it will help.