可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
The problem as follows. Let us have 3 tabs with fragments:
- Tab 1 (Fragment A). Needs to send data to Tab 2.
- Tab 2 (Fragment B). Needs to receive data from Tab 1.
- Tab 3 (Fragment B). Already contains data.
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:
- Set the unique ID for Fragment B via arguments when they were created.
- Register same
Local Broadcast Receiver
for both instances of Fragment B
- Send data from Fragment A to Fragment B with its ID
- 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.
回答1:
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)
回答2:
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.
回答3:
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.
回答4:
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
.
回答5:
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.