0

I have tried several times to send data from one fragment to another and it never works. I already used "Intent" and "Bundle" but nothing works. They said to use BroadCast Receiver, but there is little information on the Internet. Can anyone give me an example?

Bloo
  • 9
  • 1
  • 4

4 Answers4

1

There is no intent passing concept in fragment communication, fragments only accepts the arguments instead. Here is an simple example of fragment communication using bundle:

  YourReceiverFragment newFragment = new YourReceiverFragment();
  Bundle args = new Bundle();
  args.putString("key1", data1);
  args.putString("key2", data1);
  newFragment.setArguments(args);

And get it from another (receiver) fragment inside the onCreateView() like:

String value1 = getArguments().getString("key1");
// and so on

But, Another good practice for fragment to fragment communication is to use interfaces via Activity like:

SenderFragment:

    public class SenderFragment extends Fragment {

    SenderFragmentListener mCommunication;
    public SenderFragment() {}// Required empty public constructor

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mCommunication = (SenderFragmentListener) context;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sender, container, false);
        Button button = (Button) view.findViewById(R.id.btn_sender);
        // on click button
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCommunication.messageFromSenderFragment("Hello Fragment i am Sender...");
            }
        });
        return view;
    }
    //Interface for communication
    public interface SenderFragmentListener {
        void messageFromSenderFragment(String msg);
    }
    @Override
    public void onDetach() {
        super.onDetach();
        mCommunication = null;
    }
}

Activity:

    public class MainActivity extends AppCompatActivity implements 
        SenderFragment.SenderFragmentListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public void messageFromSenderFragment(String msg) {
        FragmentManager manager = getSupportFragmentManager();
        ReceiverFragment mReceiverFragment = (ReceiverFragment)manager.findFragmentById(R.id.frg_Receiver);
        mReceiverFragment.youGotMsg(msg);
    }
}

ReceiverFragment:

    public class ReceiverFragment extends Fragment {

    TextView tv_msg;

    public ReceiverFragment(){} // Required empty public constructor

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_receiver, container, false);
        tv_msg = (TextView) view.findViewById(R.id.tv_receiver);
        return view;
    }
    //Receive message
    public void youGotMsg(String msg) {
        tv_msg.setText(msg);
    }
}

Let me know if it helps.

A S M Sayem
  • 2,010
  • 2
  • 21
  • 28
  • Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference – Bloo Sep 23 '19 at 22:09
  • You are sending data to the same fragment! Here is some edit on your sender fragment: ** String data1="ola"; Parque_Fragment newFragment = new Parque_Fragment(); Bundle args = new Bundle(); args.putString("key1", data1); newFragment.setArguments(args); mContext = getActivity(); ** Replace it hope it will work. – A S M Sayem Sep 23 '19 at 22:40
  • Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference – Bloo Sep 23 '19 at 22:44
0

Fragments don't communicate directly unless you use viewmodels. Here is a guide that can help you get started in working with fragments

https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments

0

you can use viewmodel to share data between fragments, you can see detail in repo: enter link description here

Tuan Dao
  • 99
  • 5
0

Data can be passed between fragments using bundle

You have to write this in place where you are trying to change the fragment

Bundle bundle = new Bundle();
bundle.putInt("key_name1", value1);
bundle.putString("key_name2", value2);


FragmentB fragmentB = new FragmentB();
fragmentB.setArguments(bundle);

In FragmentB you can receive this data (you can write this in the onViewCreated() lifecycle of the fragment):

Bundle bundle = this.getArguments();
if (bundle != null) {

value1=bundle.getInt("key_name1", 0); //0 is the default value , you can change that
value2=bundle.getString("key_name2",""); 
}