I'm trying to pass data from one fragment to another using a callback but im having trouble doing it. In my activity I call a fragment A
, that calls fragment B
. In fragment B
i select a item and I need to pass that item to fragment A
. Can someone tell me how can I do that?
I know that with a callback I can get the data from fragment B
to the activity, but how can I send that data to fragment A
with keeping the state of that fragment?
Your activity should implement the callback, the callback can then find fragment B and post data to one of its public methods. If you make the public method in fragment B accept whatever data you need, you should be able to pass it straight through.
Something like this:
@Override
public void onCallback(String data) {
FragmentB frag = (FragmentB) getSupportFragmentManager().findFragmentByTag(FragmentB.class.getSimpleName());
if (frag != null) {
frag.myMethod(data);
}
}