I've looked in other threads, and they all show how to do this on creation of the fragment, not asynchronously.
I had 3 activities, each of these activities did an asynchronous okhttp3.HttpUrl get in order to receive data from a third party JSON API. Then when complete, it would populate the activity with data.
I have since converted these 3 activities to fragments and put them in a parent activity. However, this means every time I load the new parent activity, it does THREE okhttp3.HttpUrl fetches to populate the 3 fragments.
All three fetches go to the same URL, so I was thinking to instead put the okhttp3.HttpUrl request in the parent activity and once its done, send the entire JSON package down to the fragments. This is after creation of the fragments... so I have no idea how to do this...
Any ideas?
My Parent Activity:
public class ChallongeEvent extends AppCompatActivity {
private TextView tab_text;
private String EVENT_ID, URL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_challonge_event);
init();
}
private void init() {
tab_text = (TextView) findViewById(R.id.tab_text);
Intent intent = getIntent();
EVENT_ID = intent.getStringExtra("event_id");
if (Challonge.SUBDOMAIN.isEmpty()) {
URL = "https://api.challonge.com/v1/tournaments/" + EVENT_ID + ".json";
} else {
URL = "https://api.challonge.com/v1/tournaments/" + Challonge.SUBDOMAIN + "-" + EVENT_ID + ".json";
}
String titles[] = new String[] { getString(R.string.players), getString(R.string.matches) };
int numTabs = intent.getIntExtra("num_tabs", 1);
EventAdapter adapter = new EventAdapter(getSupportFragmentManager(), titles, numTabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setCurrentItem(intent.getIntExtra("num_tabs", 1) - 1);
SlidingTabLayout sliding_tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
sliding_tabs.setDistributeEvenly(true);
sliding_tabs.setViewPager(pager);
}
private void populate() {
AsyncGet fetch = new AsyncGet(new AsyncResponse() {
@Override
public void processFinish(String output) {
}
});
HttpUrl.Builder urlBuilder = HttpUrl.parse(URL).newBuilder();
urlBuilder.addQueryParameter("api_key", Challonge.API_KEY);
urlBuilder.addQueryParameter("include_participants", "1");
urlBuilder.addQueryParameter("include_matches", "1");
fetch.execute(urlBuilder.build().toString());
}
public void setTabText(String text) {
tab_text.setText(text);
}
}
class EventAdapter extends FragmentPagerAdapter {
private final String[] titles;
private final int numTabs;
public EventAdapter(FragmentManager fm, String mTitles[], int mNumTabs) {
super(fm);
this.titles = mTitles;
this.numTabs = mNumTabs;
}
@Override
public Fragment getItem(int position) {
switch (position)
{
case 1:
return new ChallongeMatches();
default:
return new ChallongePlayers();
}
}
@Override
public String getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return numTabs;
}
}