2

I have an Activity, and three tab each with it's own Fragments. I am loading video information from JSON into a list in Activity.

I read URL of video from list and display it in upper half of Activity layout, I have three Fragments in bottom half, the first Fragment shows info about video, second, related video, and third comments.

Problem is: I want to send the list from Activity to 3 Fragments in tabs.

I did lots of ways, using interfaces, callback,... but before fetching JSON data from url, Fragments starts and when this happens the empty list want to be set to TextViews, EditTexts, ImageViews,... in Fragment and then application stops.

How to get data from JSON and then load Fragments? Or How to simultaneously load data to Activity itself and all Fragments? Or I can fetch that JSON in activity and 3 Fragments separately, but I don't want to do that!

Please give me a tutorial or an article to learn this subject.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Davoud
  • 2,576
  • 1
  • 32
  • 53

2 Answers2

3

Register callback listeners in your fragments.

Load JSON data in your activity and set the corresponding data to static variables.

Now trigger callback listeners in fragments when you set values in static variables after parsing JSON.

In your callback methods in fragments set the data from static variables.

Update:

You can also pass your data in callback listeners methods to fragments instead of using static variable.

abdul khan
  • 843
  • 2
  • 7
  • 24
2

The best approch is to achieve it through interface and put extra but if some how you are not succed you can use below methods:

1.You can use event bus for integrating this funtionality

https://github.com/greenrobot/EventBus

  1. secondly you can do this by creating public getter setter method on parent activity and calling them on fragment by parent activity type context/object.