0

I have a tabbed activity with four tabs. Each tab has its own unique fragment. I want to collect data from the first three tabs, analyze it, and send the analyzed data to a TextView in the fourth tab. How would I do this?

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
gtvario
  • 11
  • 1
  • Keep in mind that you only need to update data in a Fragment if it's the one currently being viewed. You can use the method from the answer here to achieve what you want: http://stackoverflow.com/questions/36503779/refresh-data-in-viewpager-fragment – Daniel Nugent Jun 17 '16 at 15:32

1 Answers1

1

There's two solutions I can think of,

First, keep a reference of all the 4 tabs in your activity. From your fragments, you can access the activity with getActivity() and call the relevant update function.

public class YourActivity {
   ...
   public void updateFourthFragment(Object data) {
       fourthFragment.update();
   }
}

public class FirstFragment {
   ...
   public void onInput() {
       ((YourActivity) getActivity()).updateFourthFragment(input);
   }
}
...

Second, use an event bus, like http://square.github.io/otto/

public class FirstFragment {
    ...
    public void onInput() {
        bus.post(new InputEvent(input));
    }
}

public class FourthFragment {
    ...
    @Subscribe
    public void onInputEvent(InputEvent event) {
        handleInput(event.getInput());
    }
}
Anuj
  • 1,912
  • 1
  • 12
  • 19