Reputation: 4462
I'm looking for a solution to pass parameters between Fragments without replace. I have 2 fragments, Frag1 and Frag2. Frag1 needed some informations of Frag2 but I can't replace Frag2.
How could send parameters from Frag2 to Frag1 without replace that ?
Upvotes: 1
Views: 197
Reputation: 6530
You can create a communication channel between the 2 fragments with the help of their host activity and listeners.
The Fragment that has to send the data can use a listener implemented by the host activity, here is how it can be done:
public static class FragmentA extends ListFragment {
...
// Container Activity must implement this interface
public interface OnArticleSelectedListener {
public void onArticleSelected(int position);
}
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event and position to the host activity
mListener.onArticleSelected(position);
}
}
The code for the Activity which hosts 2 fragments, contains the listener implementation which on trigger calls the second hosted fragment.
public static class MainActivity extends Activity
implements OnArticleSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the FragmentA
// Do something here to display that article
FragmentB articleFrag = (FragmentB)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
articleFrag.doWhateverWithDataFromFragmentA(position);
}
}
Final, step the receiver Fragment which gets the data.
public static class FragmentB extends Fragment {
...
// This can also have another or the same listener to send data back,
// in case of the same, I think its better to have 2 different methods
// to avoid getting in an infinite loop.
public void doWhateverWithDataFromFragmentA(int position) {
// Got the data from A!
}
}
Source/Reference: The whole idea is based in the combination of the two tutorials from android developers: Communicating with the Activity and Communicating with Other Fragments
Generally,
This is a helpful way to transfer data on actions or the actions themselves from one fragment to another.
In case you just need to share data between the fragments consider keeping data in Host Activity and access them from both fragments. If your data object at host activity is private MySharedDataObject thisLocalPrivateObject;
then it can be referenced from the fragments with:
MySharedDataObject data = ((MyHostActivityName) getActivity()).getSharedData();
Upvotes: 1
Reputation: 7082
Please consider using event bus library like greenrobot EventBus. With it, you will be ablt to post a message from one fragment and receive it in another. It is really simple and really powerful at the same time.
https://github.com/greenrobot/EventBus
Upvotes: 2