Communicate between different instances of the same fragment

The task is as follows. We have 3 tabs with fragments:

  • Tab 1 (fragment A). You must send data to Tab 2.
  • Tab 2 (fragment B). You need to get data from tab 1.
  • Tab 3 (fragment B). Already contains data.

As you can see, Tab 3 and Tab 2 contain the same fragment, but different instances.

How to send data (not via arguments) exactly in Tab 2?

What I tried:

  • Define a unique identifier for fragment B through the arguments when they were created.
  • Register the same Local Broadcast Receiver for both instances of fragment B
  • Send data from fragment A to fragment B with its identifier
  • In fragment B onReceive() check if the recipient identifier matches the fragment ID

But, unfortunately, the transfer was sent only to Tab 3.


EDIT: additional information.

These tabs are placed inside another snippet with ViewPager . This is due to the combination of NavigationDrawer , which has a snippet with the ViewPager and Tabs in question.

+5
source share
5 answers

I suggest introducing EventBus into your application.

To add a dependency, add compile 'de.greenrobot:eventbus:2.4.0' to the list of dependencies.

Then you simply sign your third tab fragment to listen to the events from the first fragment.

Something like this: in fragment B

 @Override public void onAttach(Activity activity) { super.onAttach(activity); eventBus.register(this); } @Override public void onDetach() { eventBus.unregister(this); super.onDetach(); } @SuppressWarnings("unused") // invoked by EventBus public void onEventMainThread(NewDataEvent event) { // Handle new data } 

NewDataEvent.java

 public class NewDataEvent extends EventBase { public NewDataEvent() {} } 

And in Fragment A just send the event:

 protected EventBus eventBus; .... eventBus = EventBus.getDefault(); .... eventBus.post(new NewDataEvent()); 

(and to avoid handling the event in the second tab - just pass an additional parameter during the creation of the fragment if it should listen to the event)

+6
source

Are fragments placed in one action? Then you can implement the interface in your hosting activities.

 YourActivity implements MyInterface { ... } 

And in your snippets you define this:

 @Override public void onAttach(final Activity context) { myInterface = (MyInterface) context; } 

And when you click something in your snippet, call myInterface.doSomething(parameter); . And then your activity can delegate another fragment.

+5
source

You did not write how you add fragments at runtime or not. It would be better to add it at runtime and assign a TAG to each fragment, for example:

 ... fragmentTransaction.replace(R.id.layoutTab2, fragment, "FB-2"); ... fragmentTransaction.replace(R.id.layoutTab3, fragment, "FB-3"); ... 

so later you can identify or find the fragment by its tag. For instance:

 FragmentManager.findFragmentByTag("FB-2") 

or if you need snippet B 3:

 FragmentManager.findFragmentByTag("FB-3") 

Hope this helps.

0
source

You can use the idea of ​​a bundle to transfer data between fragments. I see that my proposal for this idea in a previous post was misunderstood. Here is a sample code in which a fragment receives data. FRAGMENT2

 public static Fragment2 newInstance(long param1, String param2) { MediaFragment fragment = new Fragment2(); Bundle args = new Bundle(); args.putLong(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { Bundle bundle = getArguments(); audioId = bundle.getLong(ARG_PARAM1); audioName = bundle.getString(ARG_PARAM2); } } 

In relation to another fragment or action causing Fragment2 :

 Fragment2 recvfragment = Fragment2.newInstance(itemId, itemName); 

Notes:

  • With Bundles, you can transfer specific data types with values ​​to different instances of the fragment that you need.
  • In this case, the code passes the type Long and String to Fragment2 .
0
source

If you are moving from fragment A to fragment B, you can simply pass the values ​​to the constructor of fragment B.

Or, if you need the same dataset that will be used in all three fragments, you can also use the share settings.

0
source

Source: https://habr.com/ru/post/1237843/


All Articles