0

I want to transfer some data like "editText1Double" and "sum" to another fragment (SecondFragment) that will use those data. How would I do that?

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.first_fragment, container, false);
    editText1 = (EditText) v.findViewById(R.id.editText1);
    editText2 = (EditText) v.findViewById(R.id.editText2);
    editText3 = (EditText) v.findViewById(R.id.editText3);
    textView = (TextView) v.findViewById(R.id.textView);


    calculateButton = (Button) v.findViewById(R.id.calculateButton);
    calculateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            editText1Double = Double.parseDouble(editText1.getText().toString());
            editText2Double = Double.parseDouble(editText2.getText().toString());
            editText3Double = Double.parseDouble(editText3.getText().toString());

            sum = editText1Double + editText2Double + editText3Double;
        }
    });
Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
gnrcs
  • 89
  • 3
  • 9
  • 4
    Possible duplicate of [How to send data from one Fragment to another Fragment?](https://stackoverflow.com/questions/24555417/how-to-send-data-from-one-fragment-to-another-fragment) – Umair Jun 08 '18 at 05:27

3 Answers3

2

1) Using Bundle :

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

and Receive as

Bundle bundle = getArguments();
Double sum = bundle.getDouble("key");

2)Using Construcor:

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B(sum);//receiving fragment
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

and Receive as

public class Fragment_B extends Fragment{
private double sum;
 public Fragment_B (double sum) {
        this.sum= sum;
    }
 public Fragment_B () {

 }}
Rohit269
  • 91
  • 4
  • 6
0

Pass data by using

fragment = new HomeFragment();
                Bundle bundle = new Bundle();
            bundle.putDouble("double", 1.1);
                fragment.setArguments(bundle);
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, "HomeFragment").addToBackStack(null).commit();

And get data in another Fragment using

Bundle bundle = getArguments();
double d = bundle.getDouble("double")

Hope it works fine for you.

Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
0

Sending data from fragment_A

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("sum_key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

Receiving data at fragment_B

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    Bundle bundle = getArguments();
        Double sum = bundle.getDouble("sum_key");
}
Chethan Kumar
  • 185
  • 1
  • 12