AnZ
AnZ

Reputation: 1060

Communicate between different instances of same fragment

The problem as follows. Let us have 3 tabs with fragments:

As you see Tab 3 and Tab 2 contain the same Fragment but different instances.

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

What I've tried:

  1. Set the unique ID for Fragment B via arguments when they were created.
  2. Register same Local Broadcast Receiver for both instances of Fragment B
  3. Send data from Fragment A to Fragment B with its ID
  4. In Fragment B onReceive() check if recevied ID equals ID of Fragment

But unfortunately broadcast was sent to Tab 3 only.


EDIT: some more information.

Those tabs are hosted inside another fragment with ViewPager. Thats due to combination of NavigationDrawer which has fragment with ViewPager and Tabs mentioned in question.

Upvotes: 6

Views: 1101

Answers (5)

TharakaNirmana
TharakaNirmana

Reputation: 10353

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

Or else if you need the same set of data to be used in all three fragments, you can use share preferences as well.

Upvotes: 0

The Original Android
The Original Android

Reputation: 6215

You can use the idea of Bundle for passing data between Fragments. I can see that my suggestion of this idea in a previous post was misunderstood. Here is sample code where the fragment receives the 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);
    }
}

On another fragment or activity, calling Fragment2:

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

Notes:

  • With Bundles, you can pass specific data types with the values into different instances of the Fragment, which you require.
  • In this case, the code is passing a Long and String type onto Fragment2.

Upvotes: 0

ivan.panasiuk
ivan.panasiuk

Reputation: 1259

You didn't write how are you adding fragments, during runtime or not. It will be better to add it during runtime and assign TAG to a each fragment, like this:

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

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

FragmentManager.findFragmentByTag("F-B-2")

or if you need fragment B 3:

FragmentManager.findFragmentByTag("F-B-3")

Hope it will help.

Upvotes: 0

Konstantin Loginov
Konstantin Loginov

Reputation: 16010

I'd suggest to introduce EventBus in your app.

To add dependency - add compile 'de.greenrobot:eventbus:2.4.0' into your list of dependencies.

Then, you just subscribe your third tab's fragment to listen to event 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 event in 2nd tab - just pass extra parameter during instantiation of fragment, if it has to listen to the event)

Upvotes: 6

Thomas R.
Thomas R.

Reputation: 8073

Are the fragments hosted in one activity? Then you could implement an interface on your hosting activity.

YourActivity implements MyInterface {
...
}

And in your fragments you define this:

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

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

Upvotes: 5

Related Questions