How to transfer some data to another Fragment
likewise it was done with extras
for intents
?
Your input fragment
public class SecondFragment extends Fragment {
EditText etext;
Button btn;
String etex;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.secondfragment, container, false);
etext = (EditText) v.findViewById(R.id.editText4);
btn = (Button) v.findViewById(R.id.button);
btn.setOnClickListener(mClickListener);
return v;
}
View.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
etex = etext.getText().toString();
FragmentTransaction transection = getFragmentManager().beginTransaction();
Viewfragment mfragment = new Viewfragment();
//using Bundle to send data
Bundle bundle = new Bundle();
bundle.putString("textbox", etex);
mfragment.setArguments(bundle); //data being send to SecondFragment
transection.replace(R.id.frame, mfragment);
transection.isAddToBackStackAllowed();
transection.addToBackStack(null);
transection.commit();
}
};
}
your view fragment
public class Viewfragment extends Fragment {
TextView txtv;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.viewfrag,container,false);
txtv = (TextView) v.findViewById(R.id.textView4);
Bundle bundle=getArguments();
txtv.setText(String.valueOf(bundle.getString("textbox")));
return v;
}
}