-1

I tried the code below, but it's not working . program crushed without giving me output. How can i send data from one fragment to another fragment in same activity? First time using fragment ` //first fragment

public class FirstFragment extends Fragment implements View.OnClickListener{


    public FirstFragment() {
    }


    Button btnSend;
    EditText etTextContainer;
    Bundle b;
    SecondFragment fragB;
    View v;
    FragmentTransaction fragmentTransaction;
    Fragment fragment;
    SecondFragment mfragment;
    String etex;

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

        v= inflater.inflate(R.layout.fragment_first2, container, false);
        btnSend=(Button)v.findViewById(R.id.btnSend);
        etTextContainer=(EditText)v.findViewById(R.id.etText);
        btnSend.setOnClickListener(mClickListener);
        return  v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            etex = etTextContainer.getText().toString();
            FragmentTransaction transection = getFragmentManager().beginTransaction();
            mfragment = new SecondFragment();
            //using Bundle to send data
            Bundle bundle = new Bundle();
            bundle.putString("key", etex);
            mfragment.setArguments(bundle); //data being send to SecondFragment
            transection.replace(R.id.tvShowTxt, mfragment);
            transection.isAddToBackStackAllowed();
            transection.addToBackStack(null);
            transection.commit();

        }
    };

    @Override
    public void onClick(View view) {

    }
}

// second fragment

public class SecondFragment extends Fragment {

    Bundle b;
    TextView tvShowText;
    String s;
    View v;

    public SecondFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v= inflater.inflate(R.layout.fragment_second, container, false);
        tvShowText = (TextView)  v.findViewById(R.id.tvShowTxt);
        Bundle bundle=getArguments();
        tvShowText.setText(String.valueOf(bundle.getString("key")));

        return  v;
    }

}`
AskNilesh
  • 67,701
  • 16
  • 123
  • 163

2 Answers2

0

It is not recommended way

All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

Suggestion

Take activity instance from onAttach() inside fragment, and then ask Activity to communicate to another fragment.

Ref: Android Documentation https://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

Tixeon
  • 930
  • 1
  • 12
  • 27
0

In FirstFragment create Bundle like this

Bundle bundle = new Bundle();
bundle.putString("key","abc"); // Put anything what you want


SecondFragment fragment2 = new SecondFragment();
fragment2.setArguments(bundle);

getFragmentManager()
      .beginTransaction()
      .replace(R.id.content, fragment2)
      .commit();

In SecondFragment

Bundle bundle = this.getArguments();

if(bundle != null){
     // handle your code here.
}

Hope this helps you.

Akshay Katariya
  • 1,464
  • 9
  • 20