As mentioned by @calvinfly, try implementing an interface. Because each individual fragment is unique and does not know about each other, the only link between them is the adapter that created them. Thus, you could set a callback between the Fragment and its Adapter:
public class DummyFragment extends Fragment {
private OnEditTextSendListener mListener;
...
public interface OnSendTextListener { // this requires the adapter to implement sendMessage()
public void sendMessage(CharSequence msg);
}
public void setOnSendTextListener(OnSendTextListener listener) {
mListener = listener;
}
public View onCreateView( ... ) {
...
(EditText) editText = (EditText) rootView.findViewById(R.id.editText);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
mListener.sendMessage(v.getText());
}
}
}
}
Notes: Specify listening to a particular actionId
if desired (such as EditorInfo.IME_ACTION_SEND
.
Then in your Pager Adapter:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter
implements DummyFragment.OnSendTextListener {
private String mReceivedMessage;
// the required method to implement from the interface
public void sendMessage(CharSequence msg) {
mReceivedMessage = msg.toString();
notifyOnDataSetChanged(); // tell the adapter views need to be updated
}
}
From here, your adapter now receives your EditText inputs when you perform an action on your EditText (once the Adapter calls setOnSendTextListener
and sets it to itself, which you'll see below). The only thing left is to pass this message back to the corresponding fragment, probably during getItem
as one of the arguments in a Bundle.
public Fragment getItem(int position) {
Fragment fragment;
Bundle args = new Bundle();
switch (position) {
case 0:
fragment = new DummyFragment();
((DummyFragment) fragment).setOnSendTextListener(this);
break;
case 1:
// your other fragment
case 2:
// your other fragment that wants the message from 1st fragment
args.putString(DummyOtherFragment.ARG_DUMMYFRAGMENT_MSG, mReceivedMessage);
break;
default:
}
// other arguments here
fragment.setArguments(args);
return fragment;
}
Hope this helps - see the Android developers guide Creating event callbacks to the activity) for more info.
Side note: You will probably encounter the issue where your Adapter correctly receives the callback info, but the fragment it is passing the data to doesn't reload with the data (even though you're calling notifyOnDataSetChanged()
. That is a whole other issue on its own and I'd like to direct you to another SO question about ViewPager refreshing fragments for further reading.