You can use Interface to pass data between fragments.
Declare an interface:
public interface OnButtonPressListener {
public void onButtonPressed(String msg,int value,ArrayList<Object> arrData);
}
on LayoutOne.java fragment:
public class LayOutOne extends Fragment {
OnButtonPressListener buttonListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
Button but=(Button)root.findViewById(R.id.button1);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonListener.onButtonPressed("Message From First Fragment",1,ArrayList objct);
}
});
return root;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
buttonListener = (OnButtonPressListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onButtonPressed");
}
}
on fragment 2
public class LayOutTwo extends Fragment implements OnButtonPressListener{
ViewGroup root;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
root = (ViewGroup) inflater.inflate(R.layout.layout_two, null);
return root;
}
@Override
public void onButtonPressed(String msg,int i,ArrayList<object> obj) {
// TODO Auto-generated method stub
//print the values or show them
}