-2

I am new to fragments. I added two edit texts in first fragment and I want to send that edittext data to second fragment.

I am using a bundle, but its printing null in second fragment.

Can anyone tell me how to send data to other fragment?

First fragment

    nextt.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View _view) {
            int viewId = _view.getId();
            FragmentTransaction ft;
            switch (viewId) {
                case R.id.Button1:
                    FragmentManager fm = getFragmentManager();
                    ft = fm.beginTransaction();
                    SecondFrag secondFrag = new SecondFrag();
                    Bundle bundle = new Bundle();
                    bundle.putInt("deviceInst",viewId);
                    secondFrag.setArguments(bundle);
                    ft.replace(R.id.total_frame_content, secondFrag);
                    ft.addToBackStack(null);
                    ft.commit();
                    break;
            }
        }
    });

In second fragment

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

    View v = inflater.inflate(R.layout.second_new, container, false);
    String value = getArguments().getString("deviceInst");
    System.out.println("TTTT"+ value);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Rajkumar
  • 3
  • 1
  • 7

4 Answers4

2

In your first fragment you pass an int as an argument.

bundle.putInt("deviceInst",viewId);

And then, in your second fragment you try to get that argument by using getArguments().getString("deviceInst")

which will fail, so to get the argument you pass you need to use getArguments().getInt("deviceInst")

For encapsulating the needed data, a good tip is to have a static newInstance() method in your fragments that requires data.

Here's a post about it.

https://plus.google.com/+AndroidDevelopers/posts/bCD7Zvd945d

malmling
  • 2,398
  • 4
  • 19
  • 33
0

You need to change like

 int value = getArguments().getInt("deviceInst");

instead of

 String value = getArguments().getString("deviceInst");
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
0

You will need to make the following changes in your code.

bundle.putString("deviceInst",editText.getText().toString());

Then in your second fragment you can get that argument by using

getArguments().getString("deviceInst")

Here editText is the instance of the edit text in the first fragment.

Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
0
As you have passed int in the bundle, you need to use getInt() in your receiver fragment.
For example:
 SecondFrag secondFrag = new SecondFrag();
                    Bundle bundle = new Bundle();
                    bundle.putInt("deviceInst",viewId);
                    secondFrag.setArguments(bundle);
getFragmentManager().beginTransaction().add(R.id.total_frame_content, secondFrag).commit();

In receiver fragment
  String value = getArguments().getInt("deviceInst");
Sabby
  • 403
  • 3
  • 15