How to pass data from one fragment to another

1

Hello

I received the following exercise:

• Fragment 1 should have two text boxes where the user can enter two numbers to be added, and a button to trigger the addition

• Fragment 2 must have a TextView in the center of the screen to display the result of the addition.

I can make the first fragment:

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private EditText campo1, campo2;
private TextView campo3;
private double n1, n2, n3;

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public frag1() {

}



public static frag1 newInstance(String param1, String param2) {
    frag1 fragment = new frag1();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    campo1 = (EditText) getActivity().findViewById(R.id.num1);
    campo2 = (EditText) getActivity().findViewById(R.id.num2);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_frag1, container, false);
}


public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
public void somar() {
    n1 = Double.parseDouble(campo1.getText().toString());
    n2 = Double.parseDouble(campo2.getText().toString());
    n3 = n1 + n2;
}

Now, how do I send the value of variable n3 to a TextView in another fragment?

    
asked by anonymous 19.04.2016 / 05:10

2 answers

4

Here is an example of how to send:

Adding to Fragment (this part you already do in your example):

  private static final String RESULTADO = "RESULTADO";
      public static SegundoFragment newInstance(int resultado) {
                SegundoFragment fragment = new SegundoFragment();
                Bundle args = new Bundle();
                args.putInt(RESULTADO , resultado);
                fragment.setArguments(args);
                return fragment;
            }

Now let's get this value in Second Fragment :

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_dois, container, false);
                TextView textView = (TextView) rootView.findViewById(R.id.seu_text);
//Exibimos o valor que veio com a TAG RESULTADO

                textView.setText(  getArguments().getInt(RESULTADO).toString() );
                return rootView;
            }
    
19.04.2016 / 05:26
-1

It is giving error in frag2 "error: can not find symbol variable RESULT"

Follow frag1's code and frag2 ...

public class frag1 extends Fragment {

    private EditText campo1, campo2;
    private TextView campo3;
    private int n1, n2, n3,n4;
    private static final String RESULTADO = "RESULTADO";

    public frag1() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_frag1, container, false);
    }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
            campo1 = (EditText) getActivity().findViewById(R.id.num1);
            campo2 = (EditText) getActivity().findViewById(R.id.num2);
            campo3 = (TextView) getActivity().findViewById(R.id.textView);

        Button b = (Button)getActivity().findViewById(R.id.btnSomar);
        b.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n1 = Integer.parseInt(campo1.getText().toString());
                n2 = Integer.parseInt(campo2.getText().toString());
                n3 = n1 + n2;
                campo3.setText(String.valueOf(n3));
            }
        });}
    public static frag2 newInstance(int resultado) {
        frag2 fragment = new frag2();
        Bundle args = new Bundle();
        args.putInt(RESULTADO , resultado);
        fragment.setArguments(args);
        return fragment;
    }
}

FRAG2 where it is accusing the error, the word RESULT inside setText is in red.

    private TextView textView2;
View rootview;
public frag2() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_frag2, container, false);
    TextView textView = (TextView) rootView.findViewById(R.id.textView2);


    textView.setText(  getArguments().getInt(RESULTADO).toString() );
    return rootView;
}
    
21.04.2016 / 00:46