0

I want to send a file path from one fragment to another, but I don't want to open the second fragment, I just want the second fragment to use the passed file path later to find the file. However, I use the code as below, the file path cannot be transferred to the second fragment. No error, but the filepath printed as "null". Could anybody help to solve my problem?

First fragment:

  Bundle bundle = new Bundle();
  bundle.putString("Path model sensor", pathsensor);
  bundle.putString("Path model label", pathLabels);
  AddFileToClassify addFileToClassify = new AddFileToClassify();
  addFileToClassify.setArguments((bundle));
  getFragmentManager().beginTransaction().add(R.id.relativeLayoutMain, addFileToClassify, TAG).commit();

Second fragment:

Fragment mFragment = getFragmentManager().findFragmentByTag("Addfilefragment");
Bundle bundle = mFragment.getArguments();
if(bundle != null) {
      pathModelLabel = bundle.getString("Path model label");
      pathModelSensor = bundle.getString("Path model sensor");}

2 Answers2

2

If Both fragment share same activity you can save an instance variable in the activity itself and can use that variable in your fragment Using ParentActivity.

sandeep dhami
  • 188
  • 1
  • 10
0

As per your question, Send data to another fragment without open it, I'd like to say that it is possible to send data from a fragment to another fragment without opening it. Let's assume that you have two active fragments and you want both to communicate each other. You can use LocalBroadcastManager or EventBus.

These resources may help you a lot:

How to use LocalBroadcastManager?

EventBus in 3 steps

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87